Android: Set a random image using setImageResource

Posted by Tom Jones on Stack Overflow See other posts from Stack Overflow or by Tom Jones
Published on 2010-06-11T22:23:36Z Indexed on 2010/06/11 22:32 UTC
Read the original article Hit count: 853

Filed under:

I need a help with setting a random image using setImageResource method.
In the drawable folder, I have a jpeg file named photo0.jpg, photo1.jpg...photo99.jpg.
And the following code works:

int p = R.drawable.photo1;
image.setImageResource(p);

The above will display photo1.jpg but I want to show a random image.
I tried the following but it doesn't work.

String a = "R.drawable.photo";
int n = (int) (Math.random()*100)
String b = Integer.toString(n);
String c = a+b;
int p = Integer.parseInt(c);//checkpoint
image.setImageResource(p);

It seems like the string "R.drawable.photoXX" isn't being changed to integer at the checkpoint.
Could someone please teach me a right code?
Thank you in advance.

© Stack Overflow or respective owner

Related posts about android