tags:

views:

59

answers:

2

Hi folks

i want to get image resource id by its name

Thanks in advance

Aswan

A: 

It will be something like:

R.drawable.resourcename

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.

Update:

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.

Rabid
Thankq for your reply .R.drawable.resourcename i am using now i need to get its integer value by passing resourcename
Aswan
`R.drawable.resourcename` *is* the integer.
Rabid
Hi Rabid.what you said that's is there any way by accessing R.drawable .resource value by passing resouce
Aswan
i need that integer value by passing resourcename dynamically
Aswan
Thankq like this i want and i want to get drawable resource id.how i wil to it
Aswan
As maragues has suggested, it would be like `this.context.getResources().getIdentifier("package:type/entry", null, null);` where `"package:type/entry"` would be for example, `"com.your.package:drawable/resourcename"` ?
Rabid
+3  A: 

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)

Maragues