3

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

Registered User
  • 2,239
  • 3
  • 32
  • 58

4 Answers4

5

You can simply store the ID of your resources!

public void onRollClick(View view) {
    int[] images= {R.drawable.dice1, R.drawable.dice2, R.drawable.dice3, R.drawable.dice4, R.drawable.dice5, R.drawable.dice6};
    int diceValue=new Random().nextInt(6);
    ImageView diceImage= (ImageView) findViewById(R.id.imageView);
    diceImage.setImageResource(images[diceValue]);
}
pdegand59
  • 12,776
  • 8
  • 51
  • 58
1

I'm just suggesting to use a image loading library like Picasso right away. This makes the performance a lot better and is super simple to implement. You can get the library here: http://square.github.io/picasso/ and this would be your code to go with:

public void onRollClick(View view) {
    int[] images= {R.drawable.dice1, R.drawable.dice2, R.drawable.dice3, R.drawable.dice4, R.drawable.dice5, R.drawable.dice6};
    int diceValue=new Random().nextInt(6);
    ImageView diceImage= (ImageView) findViewById(R.id.imageView);
    Picasso.with(this).load(images[diceValue]).into(diceImage);
}

Edit: You should definitely bump up your API version ;)

0

You can use getResources().getIdentifier() for get and id from a resources name. More info here java android getResources().getIdentifier() and here https://developer.android.com/reference/android/content/res/Resources.html#getIdentifier(java.lang.String,%20java.lang.String,%20java.lang.String)

Community
  • 1
  • 1
an_droid_dev
  • 1,136
  • 14
  • 18
  • 1
    In this case, get resources by name is not a suitable solution, @pdegand59 's answer is easier. – sagix Aug 23 '16 at 14:41
  • @sagix Why it's not suitable? The solution can be `diceImage.setImageResource(getResources().getIdentifier(getimages[diceValue],"drawable",getPackageName());` – an_droid_dev Aug 23 '16 at 14:45
  • It is more complicated solution than a list of resource ids. – sagix Aug 23 '16 at 14:57
0
public void onRollClick(View view) {
    int[] images={R.drawable.dice1,R.drawable.dice2,R.drawable.dice3,R.drawable.dice4,R.drawable.dice5,R.drawable.dice6};
    int diceValue=new Random().nextInt(6);
    ImageView diceImage= (ImageView) findViewById(R.id.imageView);
    diceImage.setImageResource(images[diceValue]);
}

Instead of string array create array of int drawables. so you can directly use them.

I have edited your function.

Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80