1

i got this :

String input = "image_to_show"

and in my drawables:

image_to_show.png

i do:

imageView.setImageResource(R.drawable.image_to_show);   

this works and refers to the object in drawables

can i get it to work using the string with the contexts being the same name as the drawables object

cxzp
  • 652
  • 2
  • 14
  • 28

2 Answers2

3

First, find resId by name

int resId = getResources().getIdentifier(resName, "drawable", getPackageName());

then use the found resId

imageView.setImageResource(resId);   
Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
0

You should use Resources getIdentifier method to get the relevant id.

after you get that id, call imageView.setImageResource(R.drawable.image_to_show); as usual

dors
  • 5,802
  • 8
  • 45
  • 71