How do I set an ImageViews source programmatically in Android?

Android

Android Problem Overview


I am making a compound ui element that takes an Object (a player) and I want to pick the corresponding image for the team this player plays for(the player object has a string value for its image number. I have the images in the resources folder of my project.

How do I specify the correct source for each player. Is it a case of writing a huge number of if's and elses (checking the string value) and using res.getDrawable(R.drawable.no1) etc or is there a more elegant solution where I can use a string in some way to specify the path to the source?

Android Solutions


Solution 1 - Android

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: https://stackoverflow.com/questions/4730100/android-and-getting-a-view-with-id-cast-as-a-string

Solution 2 - Android

Solution 3 - Android

If I understand correctly, you want to display an image resource from a string. I do that in an app where the user can choose an image from a custom ListPreference and it is displayed in the MainActivity layout. The drawable resource ID is stored in the SharedPreferences as a string that matches the drawable resource ID (String) example: "@drawable/logo_image". I pull the value of the ListPreference with:

    SharedPreferences shared = getSharedPreferences("com.joshuariddle.recoveryworkscounter.settings", MODE_PRIVATE);
            String logo_id = (shared.getString("pref_logo",""));

This returns the drawable resource as a String i.e. @drawable/logo_image. Then to insert that drawable/image into my layout I use:

    ImageView iv_logo = (ImageView) findViewById(R.id.imgLogo);
    iv_logo.setImageResource(getResources().getIdentifier(logo_id, "drawable", "com.yourpackage"));
    

This will change the ImageView resource to the new drawable with setImageResource() using the int returned by the method below which returns an ID (int) from a string representing the drawable resource in com.yourpackage:

    getResources().getIdentifier(logo_id, "drawable", "com.yourpackage")
    

You can also use this same method to change other resources that use drawable such as Layout backgrounds etc. You just have to use this method to get the ID as an int:

    getResources().getIdentifier("Resource Id String", "drawable", "com.yourpackage") 
    

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionblackaardvarkView Question on Stackoverflow
Solution 1 - AndroidCristianView Answer on Stackoverflow
Solution 2 - AndroidMohit DeshpandeView Answer on Stackoverflow
Solution 3 - AndroidOGZCoderView Answer on Stackoverflow