views:

228

answers:

2

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

+1  A: 

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!

st0le
A: 

Ya StOle is right.. Using an int array can solve the problem. You just need to get a incrementer variable to access particular image

success_anil