Reputation: 47
I would like to make TextView
counter with my own graphic numbers. How can I do this? Example: integer number will be 14356
, so I would like to show this number in TextView
as a Drawable
:
number1.png + number4.png + number3.png + number5.png + number6.png
Can somebody help me with some hint/tips how to code it? I don't need code, just some tips, or hints. I would like to code myself, just don't know how to start this problem. Thank you very much.
Upvotes: 0
Views: 62
Reputation: 7111
Create a layout file with a LinearLayout
and a few ImageViews
in it:
<LinearLayout orientation="horizontal" ...>
<ImageView id="@+id/digit_1000" .../>
<ImageView id="@+id/digit_100" .../>
<ImageView id="@+id/digit_10" .../>
<ImageView id="@+id/digit_1" .../>
</LinearLayout>
Use as many ImageView
as needed. Then when you update the number, you can do something like this:
private int[] digitDrawables = {R.drawable.digit_0, R.drawable.digit_1, ...}
private ImageView[] digitViews;
private void initialize() {
// Get image views from layout by ID.
int[] ids = new int[]{R.id.digit_1, R.id.digit_10, R.id.digit_100, R.id.digit_1000};
digitViews = new ImageView[4];
for (int i = 0; i < 4; i++) {
digitViews[i] = findViewById(ids[i]);
}
}
private void updateNumber(int number) {
String str = String.valueOf(number);
// Go over each digit in string, updating the image views with:
// -> digitViews[i].setImageResource(digitDrawables[digit]);
// Hide the image views that you don't need with:
// -> digitViews[i].setVisibility(View.GONE);
}
You can start from there.
Upvotes: 2
Reputation: 453
SO I would suggest doing this with Constraint Layout Horizontal Chain(in packed Configuration) of Image Views.
In the code you can just update a counter within a loop with 1-sec delay if its count down timmer else do it the way you indent to(game points) and if conditions checking if the new number changes 10's place, 100's place ....100000's place and update accordingly.
Upvotes: 1