Sign up ×

I want to access a resource like a String or a Drawable by its name and not its int id.

Which method would I use for this?

share|improve this question
possible duplicate of How can i get the resource id of an image if I know its name? –  ghoti Dec 7 '12 at 18:55

5 Answers 5

up vote 58 down vote accepted

It will be something like:


Make sure you don't have the Android.R namespace imported as it can confuse Eclipse (if thats what you're using).

If that doesn't work, you can always use a context's getResources method ...

Drawable resImg = this.context.getResources().getDrawable(R.drawable.resource);

Where this.context is intialised as an Activity, Service or any other Context subclass.


If it's the name you want, the Resources class (returned by getResources()) has a getResourceName(int) method, and a getResourceTypeName(int)?

Update 2:

The Resources class has this method:

public int getIdentifier (String name, String defType, String defPackage) 

Which returns the integer of the specified resource name, type & package.

share|improve this answer
Thankq for your reply .R.drawable.resourcename i am using now i need to get its integer value by passing resourcename –  Aswan Aug 13 '10 at 11:45
R.drawable.resourcename is the integer. –  Rabid Aug 13 '10 at 11:46
Hi Rabid.what you said that's is there any way by accessing R.drawable .resource value by passing resouce –  Aswan Aug 13 '10 at 11:47
i need that integer value by passing resourcename dynamically –  Aswan Aug 13 '10 at 11:48
Thankq like this i want and i want to get drawable resource i wil to it –  Aswan Aug 13 '10 at 11:57

If I understood right, this is what you want

int drawableResourceId = this.getResources().getIdentifier("nameOfDrawable", "drawable", this.getPackageName());

Where "this" is an Activity, written just to clarify.

In case you want a String in strings.xml or an identifier of a UI element, substitute "drawable"

int resourceId = this.getResources().getIdentifier("nameOfResource", "id", this.getPackageName());

I warn you, this way of obtaining identifiers is really slow, use only where needed.

Link to official documentation: Resources.getIdentifier(String name, String defType, String defPackage)

share|improve this answer
this is the solution I wanted :D –  Kreshnik Apr 19 '14 at 19:39
This is pretty useful in the context of writing tests to make sure certain strings exist or etc. –  Ehtesh Choudhury May 1 at 22:14
int resourceID = 
    this.getResources().getIdentifier("resource name", "resource type as mentioned in",this.getPackageName());
share|improve this answer
It'd be even better if you explained the code you posted. –  user1114055 Oct 21 '12 at 1:11

I would suggest you using my method to get a resource ID. It's Much more efficient, than using getIdentidier() method, which is slow.

Here's the code:

     * @author Lonkly
     * @param variableName - name of drawable, e.g R.drawable.<b>image</b>
     * @param с - class of resource, e.g R.drawable.class or R.raw.class
     * @return integer id of resource
    public static int getResId(String variableName, Class<?> с) {

        Field field = null;
        int resId = 0;
        try {
            field = с.getField(variableName);
            try {
                resId = field.getInt(null);
            } catch (Exception e) {
        } catch (Exception e) {
        return resId;

share|improve this answer
It will not work for all cases. For example if you have <string name="">content</string> than the R.string class will have a string_name field. And your method will not work in this point. –  ddmytrenko Oct 22 '13 at 14:32
Also your method is not fast actually. Because Java class serialization is never working quick. –  ddmytrenko Oct 23 '13 at 10:39

A simple way to getting resource ID from string. Here resourceName is the name of resource ImageView in drawable folder which is included in XML file as well.

int resID = getResources().getIdentifier(resourceName, "id", getPackageName());
        ImageView im = (ImageView) findViewById(resID);
        Context context = im.getContext();
        int id = context.getResources().getIdentifier(resourceName, "drawable",
share|improve this answer

Your Answer


