1

I want to set an ImageView programatically, passing the name of the local resource drawable as String.

The drawable clearly are int identified in this way

 R.drawable.mydrawable_name

How could I do to solve this problem without map everything in a conditional switch?

I want to avoid something in this form

if(myString.equal"stringname_1"){
   myImageview.setImageResource( R.drawable.stringname_1);
}
else if(myString.equal"stringname_1")....
etc
AndreaF
  • 11,975
  • 27
  • 102
  • 168
  • so what will be your input for the drawable you written – stinepike Jul 03 '13 at 17:23
  • I think this might help. http://stackoverflow.com/questions/17328376/what-path-format-should-i-use-with-bitmapfactory-decodefilestring-pathname/17349998#17349998 – DeeV Jul 03 '13 at 17:25

3 Answers3

11

You can use getResources().getIdentifier() (http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier(java.lang.String, java.lang.String, java.lang.String)) for this purpose. For your code it would look something like this:

int id = getResources().getIdentifier(myString, "drawable", getPackageName());
Drawable drawable = getResources().getDrawable(id);
myImageview.setImageDrawable(drawable);

The code above assumes you're in an Activity.

  • thanks but unfortunately I'm not in an activity and cannot compile due to getResources() results not defined – AndreaF Jul 03 '13 at 17:54
  • From where are you calling this code then. Passing through a reference to a context or activity should be fairly straight forward. Post more of your code in case you need help with it. –  Jul 03 '13 at 17:59
  • this code is used in a loader for a custom list view, so if I have understood should I pass the context from the activity that shows the list view to the listview manager and from the listview manager to loader? – AndreaF Jul 03 '13 at 18:04
  • Yes. Passing through the activity through the chain works. You might also want to expose a getActivity() method in your listview manager and hold on to it there instead of passing it through all the way. depends on your preference –  Jul 03 '13 at 18:12
  • 1
    just want to mention here, that `mystring` will only contains the filename/imagename and NOT `R.drawable.filename` – Himanshu Aggarwal Nov 20 '14 at 04:11
1

You can do it using java reflection

        Field field = R.drawable.class.getDeclaredField("mydrawable_name");

        int a = field.getInt(this);

a will have the id value of R.drawable.mydrawable_name . Now you can use this id to set image

stinepike
  • 54,068
  • 14
  • 92
  • 112
0

Pl Note: Below one is need to be comment for the accepted answer but I dont have previlage to add comment.

getDrawable(id) is deprecated now instead we can use

getDrawable(int id, Resources.Theme theme)

Return a drawable object associated with a particular resource ID and styled for the specified theme.

getTheme return the current theme.

Sample is given below:

Drawable drawable = resources.getDrawable(id, context.getTheme());
slfan
  • 8,950
  • 115
  • 65
  • 78
Divyaadz
  • 175
  • 2
  • 11