Skip to content

Commit

Permalink
Allow setting default text and character list via XML (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinatonic authored Sep 21, 2017
1 parent 70e4cb2 commit 4127488
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public abstract class BaseActivity extends AppCompatActivity {
protected void onResume() {
super.onResume();
resumed = true;
handler.post(createRunnable());
handler.postDelayed(createRunnable(), 1000);
}

private Runnable createRunnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import java.util.Random;

public class MainActivity extends BaseActivity {
private static final char[] NUMBER_LIST = TickerUtils.getDefaultNumberList();
private static final char[] CURRENCY_LIST = TickerUtils.getDefaultListForUSCurrency();
private char[] alphabetlist;

private TickerView ticker1, ticker2, ticker3;
Expand All @@ -30,12 +28,11 @@ protected void onCreate(Bundle savedInstanceState) {
}
}

ticker1 = (TickerView) findViewById(R.id.ticker1);
ticker2 = (TickerView) findViewById(R.id.ticker2);
ticker3 = (TickerView) findViewById(R.id.ticker3);
ticker1 = findViewById(R.id.ticker1);
ticker2 = findViewById(R.id.ticker2);
ticker3 = findViewById(R.id.ticker3);

ticker1.setCharacterList(NUMBER_LIST);
ticker2.setCharacterList(CURRENCY_LIST);
ticker2.setCharacterList(TickerUtils.getDefaultListForUSCurrency());
ticker3.setCharacterList(alphabetlist);

findViewById(R.id.perfBtn).setOnClickListener(new View.OnClickListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_perf);

final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
final RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(new TestAdapter());
Expand Down Expand Up @@ -64,20 +64,20 @@ public int getItemCount() {
private class TickerViewHolder extends RecyclerView.ViewHolder {
private TickerView ticker1, ticker2, ticker3, ticker4;

public TickerViewHolder(View itemView) {
TickerViewHolder(View itemView) {
super(itemView);
ticker1 = (TickerView) itemView.findViewById(R.id.ticker1);
ticker2 = (TickerView) itemView.findViewById(R.id.ticker2);
ticker3 = (TickerView) itemView.findViewById(R.id.ticker3);
ticker4 = (TickerView) itemView.findViewById(R.id.ticker4);
ticker1 = itemView.findViewById(R.id.ticker1);
ticker2 = itemView.findViewById(R.id.ticker2);
ticker3 = itemView.findViewById(R.id.ticker3);
ticker4 = itemView.findViewById(R.id.ticker4);

ticker1.setCharacterList(CHAR_LIST);
ticker2.setCharacterList(CHAR_LIST);
ticker3.setCharacterList(CHAR_LIST);
ticker4.setCharacterList(CHAR_LIST);
}

public void update(boolean animate) {
void update(boolean animate) {
ticker1.setText(getRandomNumber(8), animate);
ticker2.setText(getRandomNumber(8), animate);
ticker3.setText(getRandomNumber(8), animate);
Expand Down
4 changes: 3 additions & 1 deletion ticker-sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="2"
android:text="1234567"
android:textColor="?colorPrimary"
android:textSize="50sp"
app:ticker_animationDuration="500" />
app:ticker_animationDuration="500"
app:ticker_defaultCharacterList="number" />

<com.robinhood.ticker.TickerView
android:id="@+id/ticker2"
Expand Down
7 changes: 4 additions & 3 deletions ticker/src/main/java/com/robinhood/ticker/TickerUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ public static char[] getDefaultListForUSCurrency() {
}

/**
* @return a default ordering for numbers (including periods).
* @return a default ordering for numbers.
*/
public static char[] getDefaultNumberList() {
final char[] charList = new char[11];
final char[] charList = new char[12];
charList[0] = EMPTY_CHAR;
charList[1] = (int) '.';
for (int i = 0; i < 10; i++) {
charList[i + 1] = (char) (i + 48);
charList[i + 2] = (char) (i + 48);
}
return charList;
}
Expand Down
14 changes: 14 additions & 0 deletions ticker/src/main/java/com/robinhood/ticker/TickerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@ protected void init(Context context, AttributeSet attrs, int defStyleAttr, int d
setTextColor(styledAttributes.textColor);
setTextSize(styledAttributes.textSize);

final int defaultCharList =
arr.getInt(R.styleable.ticker_TickerView_ticker_defaultCharacterList, 0);
switch (defaultCharList) {
case 1:
setCharacterList(TickerUtils.getDefaultListForUSCurrency());
break;
case 2:
setCharacterList(TickerUtils.getDefaultNumberList());
break;
}
setText(styledAttributes.text, false);

arr.recycle();

animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
Expand All @@ -185,6 +197,7 @@ private class StyledAttributes {
float shadowDx;
float shadowDy;
float shadowRadius;
String text;
int textColor;
float textSize;
int textStyle;
Expand All @@ -204,6 +217,7 @@ void applyTypedArray(TypedArray arr) {
shadowDy = arr.getFloat(R.styleable.ticker_TickerView_android_shadowDy, shadowDy);
shadowRadius = arr.getFloat(R.styleable.ticker_TickerView_android_shadowRadius,
shadowRadius);
text = arr.getString(R.styleable.ticker_TickerView_android_text);
textColor = arr.getColor(R.styleable.ticker_TickerView_android_textColor, textColor);
textSize = arr.getDimension(R.styleable.ticker_TickerView_android_textSize, textSize);
textStyle = arr.getInt(R.styleable.ticker_TickerView_android_textStyle, textStyle);
Expand Down
5 changes: 5 additions & 0 deletions ticker/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
<declare-styleable name="ticker_TickerView">
<attr name="ticker_animationDuration" format="reference|integer" />
<attr name="ticker_animateMeasurementChange" format="reference|boolean" />
<attr name="ticker_defaultCharacterList" format="enum">
<enum name="us_currency" value="1" />
<enum name="number" value="2" />
</attr>

<!-- Custom implementations of common android text attributes -->
<attr name="android:gravity" tools:ignore="ResourceName" />
<attr name="android:shadowColor" tools:ignore="ResourceName" />
<attr name="android:shadowDx" tools:ignore="ResourceName" />
<attr name="android:shadowDy" tools:ignore="ResourceName" />
<attr name="android:shadowRadius" tools:ignore="ResourceName" />
<attr name="android:text" tools:ignore="ResourceName" />
<attr name="android:textAppearance" tools:ignore="ResourceName" />
<attr name="android:textColor" tools:ignore="ResourceName" />
<attr name="android:textSize" tools:ignore="ResourceName" />
Expand Down

0 comments on commit 4127488

Please sign in to comment.