2

I would like to R.drawable which is in a dynamic string format currently to an integer format.How can be it done.Please guide me with a snippet on this to resolve it.Thank you.

Integer[] images={R.drawable.apple,R.drawable.mango,R.drawable.banana};
Karthik
  • 4,943
  • 19
  • 53
  • 86

2 Answers2

14

You can use this

String mDrawableName = "myimg";
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());

or

String imageName = "picture";
int resID = getResources().getIdentifier(imageName, "drawable", "package.name");
ImageView image;
image.setImageResource(resID);
edwoollard
  • 12,245
  • 6
  • 43
  • 74
Devangi Desai
  • 1,355
  • 12
  • 17
10

You can use this

public int[] getIntIds(Integer[] images){
    int[] temp = new int[images.length];
    String name;
    for(int i=0; i<= images.length; i++){
        name = getResources().getResourceEntryName(images[i]);
        temp[i] = getResources().getIdentifier(name , "drawable", getPackageName());
    }
    return temp;
}
Girish Nair
  • 5,148
  • 5
  • 40
  • 61