tags:

views:

50

answers:

1
+1  Q: 

Android Random.

Hi All,

String rank[] = {"tclub1.png", "tclub2.png", "tclub3.png", "tclub4.png", "tclub5.png", "tclub6.png", "tclub7.png", "tclub8.png", "tclub9.png", "tclub10.png", "tclub11.png", "tclub12.png", "tclub13.png"};

   Random randInt = new Random();
    int  b = randInt.nextInt((rank.length));

    String d=("tclub"+b+".png");

    Log.v(LOG_TAG,"in value:="+d);


}  

Above is the code. Actually my array is giving me one random index between(0 to 12)..after that i m appending it to make an image name. eg(tclub1.png) Now the image name which it gives to me is String fromat. how can i assign thhis image now randomly??

A: 

If you want to load the image to an ImageView, you can do this:

String imgName = "tclub1"; // the image you want to load
int id = getResources().getIdentifier(imgName, "drawable", getPackageName());  
imageView.setImageResource(id); 
Itsik
hi itsik..thanks..but its not happening.when i use log tag to see the value of ID it is showing 0nd not setting the image randomly
vivek_Android
Are all the images located in res/drawable? Also, the image name should be without ".png". (It should only be the resource name)
Itsik
yes all the images are in drawable-hpdi folder.
vivek_Android
thanks a lotttttttttttttttttttttttt itsik..its done with ur advice.god bless u
vivek_Android
No problem. Please accept the answer by clicking the check mark.
Itsik