How do I set an ImageViews source programmatically in Android? [duplicate]

Solution 1:

I'd put the relation between Strings and images in a Map:

Map<String, Integer> map = new HashMap<String, Integer>();
map.put("blah", R.drawable.blah);
// etc...

Then, you can use the setImageResource(int) method:

ImageView image;
image.setImageResource(map.get("blah"));

Or, if the strings have the same name than the image (like in the case before), you can load the resource by using this method: Android and getting a view with id cast as a string

Solution 2:

Use setImageResource(int)