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.

4
  • Or should I use a switch statement in this case? Commented Nov 24, 2019 at 11:05
  • Possible duplicate of How to set the image from drawable dynamically in android? Commented Nov 24, 2019 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. Commented Nov 24, 2019 at 11:32
  • @VoHoTv look at my answer maybe it's the solution you need Commented Nov 25, 2019 at 10:24

1 Answer 1

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());
           )
      );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.