Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Random resources

Tags:

android

random

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 format. How can I assign this image now randomly?

like image 901
vivek_Android Avatar asked Apr 27 '26 04:04

vivek_Android


1 Answers

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); 
like image 111
Itsik Avatar answered Apr 29 '26 18:04

Itsik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!