I want to setImageResource for an ImageButton programatically, based on a variable.
For eg: if size=5, I want to setImageResource to R.drawable.five
if size=6, I want to setImageResource to R.drawable.six
Unfortunately, I have too many of these, so an if-else or switch gets tiring.
Is there a way to achieve something like: R.drawable.size?
Thanks Chris
Store the id's in an array
final int[] imgSizeIds = new int[]{ R.drawable.zero,R.drawable.one,R.drawable.two, .... };
then,
setImageResource(imgSizeIds [ size ] );
Cheers!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With