Find drawable by string [duplicate]

Solution 1:

Yes, you can look it up by name using Resources.getIdentifier().

Context context = imageView.getContext();
int id = context.getResources().getIdentifier("picture0001", "drawable", context.getPackageName());
imageView.setImageResource(id);

It's not efficient, but it works to look up occasional resources.

Solution 2:

You can also use reflection like this:

Class c = Class.forName("your.project.package.R");
Field f = c.getDeclaredField("drawable");
Class d = f.getDeclaringClass();
Field f2 = d.getDeclaredField("yourstring");
int resId = f2.getInt(null);
Drawable d = getResources().getDrawable(resId);

Though, the best solution is what MarvinLabs suggested.