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

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)