Skip to content Skip to sidebar Skip to footer

Android: Drawable Id Change After Resources Modify

I have an android application that use a gallery component to choose an icon and assign it to a button. The icon set is located in res/drawable folder and is accessed in the galler

Solution 1:

You can store the name of the drawable in the database if you don't plan to change that. getResourceEntryName returns the name from the resource id:

Resourcesresources= getResources();
Stringname= resources.getResourceEntryName(R.drawable.icon);

And you can get back the resource id from the name with getIdentifier:

int resId = resources.getIdentifier(name, "drawable", "your.package.name");

Solution 2:

You can't use static int for resource identifier, however you should look at two methods od Resources class:

getIdentifier()

getresourceName()

Solution 3:

You shouldn’t rely on the actual values of the R.drawable.* attributes. Create your own ids instead (for example 1 correspond to R.drawable.icon_home and 2 correspond to R.drawable.icon_home2)

Edit: String name and reflection should work too, but it’s probably a little overkill you have only a few icons.

Post a Comment for "Android: Drawable Id Change After Resources Modify"