With the following code you can load images in your drawable folder dynamically by giving the filename as a String. For that you have to use getResources().getIdentifier which has parameters as the “path”,”drawable” and the “package name”. This returns a unique resource ID which can be used to set the image for a ImageView using


public void changeImage(String path)
          int imgID = getResources().getIdentifier(path, "drawable",
    catch(Exception e)
         Toast.makeText(MyActivity.this,e.getMessage() + "Error : ",


  1. Pingback: How to set variable in RefID - Android Forums

  2. Dotnet July 6, 2011 at 4:25 pm

    Nice article …

  3. nicky November 8, 2011 at 9:03 pm

    on line 6 it says “your_package_name_here” for the package name, do I have to use it exactly like that or do I have to change it to the package name of my project?

    • James November 8, 2011 at 10:10 pm

      No you should use your own current package name, i.e the package name of your project.


Leave a Reply

Your email address will not be published. Required fields are marked *




Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.