I’m trying to set the source of my image, but I get an error.
String test1 = pref.getString("test", "ERROR");
String drawable1 = "R.drawable."+test1;
myImage.setImageResource(drawable1);
I’ve tried setting int drawable1 = "R.drawable."+test1;, but it still doesn’t work. I know it’s a mismatch of types, but I can’t seem to figure out a way to get it done.
It seems you could break the problem down even further and say how do you make this work?
String drawable1 = "R.drawable.myImage";
myImage.setImageResource(drawable1);
Any ideas?
You need to use
getIdentifier()to properly build resource ids from Strings:(There are now two other answer that suggest this same thing… Did I miss something or did they just feel like repeating what’s already been stated?)
As a note, if your Image is large or causes a noticable delay while loading the documentation on
setImageResources()has alternate suggestions: