Using setImageDrawable dynamically to set image in an ImageView

Arun picture Arun · Dec 27, 2011 · Viewed 332.4k times · Source

I am generating a string from database dynamically which has the same name of image in drawable folder.

Now I want to set that value for ImageView using setImageDrawable(R.id.StringGenerated) dynamically.

Any Suggestions..

Answer

King RV picture King RV · Dec 27, 2011

Try this,

int id = getResources().getIdentifier("yourpackagename:drawable/" + StringGenerated, null, null);

This will return the id of the drawable you want to access... then you can set the image in the imageview by doing the following

imageview.setImageResource(id);