0
double randomDiceNumber = Math.ceil(Math.random() * 6);
String imageSource = "ic_dice" + Double.toString(randomDiceNumber);
dice.setImageResource(R.drawable.imageSource);

What I am trying to do is set an Imageview to a different dice side depending on which number is rolled.

  • Or should I use a switch statement in this case? –  Nov 24 '19 at 11:05
  • Possible duplicate of [How to set the image from drawable dynamically in android?](https://stackoverflow.com/questions/11737607/how-to-set-the-image-from-drawable-dynamically-in-android) – George Z. Nov 24 '19 at 11:06
  • @GeorgeZ. I saw that one, but since I am new to Java I didn't manage to figure it out with that post. –  Nov 24 '19 at 11:32
  • @VoHoTv look at my answer maybe it's the solution you need – ZINE Mahmoud Nov 25 '19 at 10:24

1 Answers1

0
double randomDiceNumber = Math.ceil(Math.random() * 6);    
String uri = "@drawable/ic_dice" + Double.toString(randomDiceNumber); 
dice.setImageResource(
           getResources().getDrawable(
                  getResources().getIdentifier(uri, null, getPackageName());
           )
      );
ZINE Mahmoud
  • 1,116
  • 1
  • 11
  • 28