I have an ImageView in which I want to set an image depending upon a random value
What I know is I can set an image like this
public void onRollClick(View view) {
String[] images={"dice1.png","dice2.png","dice3.png","dice4.png","dice5.png","dice6.png"};
int diceValue=new Random().nextInt(6);
ImageView diceImage= (ImageView) findViewById(R.id.imageView);
diceImage.setImageResource(R.drawable.dice5);
}
where onClick
method is called on a Button
click. All images are in drawable
directory. Currently, I always set image dice5.png
. How could I instead set, images[diceValue]
image?
Note: I am using API 22