How do I set an ImageViews source programmatically in Android?

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?

  • Auto start application after boot completed in Android
  • GridView - sometimes rows are top-aligned, sometimes they're bottom-aligned!
  • Error matching with ORB in Android
  • Android Design Lib CollapsingToolbarLayout stops interact when horizontal RecyclerView is touched
  • integrate facebook with like button in android and iphone
  • Android Studio “Unable to save plugin settings”
  • Related posts:

    Capturing stdout/stderr with NDK
    Android application manager shows wrong app name
    How to generate Retrofit client library from wp rest api using swagger
    Back Projection in Java with OpenCV
    How to delete from firebase realtime database?
    How can I concatenate static strings with XML string resources?
  • Build error after migrating from Crashlytics SDK to Fabric
  • Numeric Soft Keyboard on Android
  • Manage Android Device through web browser from a laptop using WiFI
  • AsyncTask and error handling on Android
  • Implementing pinch zoom and drag using Android's build in gesture listener and scale listener
  • How to suppress “warning: Ignoring InnerClasses attribute for an anonymous inner class” with Gradle?
  • 3 Solutions collect form web for “How do I set an ImageViews source programmatically in 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: Android and getting a view with id cast as a string

    Use setImageResource(int)

    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") 
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.