1

Let's say I want to access to my image : R.drawable.character

I have String drawableName = character

Is there a way to do something like that or it is forbidden in Java =>

R.drawable.drawableName to access R.drawable.character ?

I found this but it is for sql database : How to get the image from drawable folder in android?

  • See [this question about how to get drawable resources…](https://stackoverflow.com/questions/29041027/android-getresources-getdrawable-deprecated-api-22/29041466) – deHaar Aug 05 '17 at 11:15
  • Didn't found what I was lookin' for in it. – Sanchez Tanguy Aug 05 '17 at 11:21
  • `ImageView yourImgView = new ImageView();` `yourImgView.setImageDrawable(getResources().getDrawable(R.drawable.character, getApplicationContext().getTheme()));` – deHaar Aug 05 '17 at 11:25
  • I'm sorry I don't understand in what it does what I was looking for. Is myimage a variable like in my example ? I'm not looking to display a resource but to call it by its name contained in a variable. – Sanchez Tanguy Aug 05 '17 at 11:26
  • You want to access the drawable, but what are you planning to do? The above comment shows how to add the resource to a GUI element that displays an image… If that is not what you want, then please explain it more clearly ;-) – deHaar Aug 05 '17 at 11:28
  • I think my example is pretty clear => Having `R.drawable.character` and doing something like : `String drawableName = character; R.drawable.drawableName;` to access dynamically `R.drawable.character` ^^ – Sanchez Tanguy Aug 05 '17 at 11:35
  • OK, then I got it wrong, sry... I see you got the right answer, good luck with your project. – deHaar Aug 05 '17 at 11:36
  • Thanks ! And thanks for taking your time for me :) – Sanchez Tanguy Aug 05 '17 at 11:44

1 Answers1

1
int id = getResources().getIdentifier(drawableName , type, package);

This will get you the ID of the resource you are looking for. With it, you can then access the resource from the R class.