Skip to content Skip to sidebar Skip to footer

How To Convert R.drawable String To R.drawable Integer Format In Android?

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. Int

Solution 1:

You can use this

StringmDrawableName="myimg";
intresID= getResources().getIdentifier(mDrawableName , "drawable", getPackageName());

or

StringimageName="picture";
intresID= getResources().getIdentifier(imageName, "drawable", "package.name");
ImageView image;
image.setImageResource(resID);

Solution 2:

You can use this

publicint[] getIntIds(Integer[] images){
    int[] temp = newint[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;
}

Post a Comment for "How To Convert R.drawable String To R.drawable Integer Format In Android?"