How do I get the resource id of an image if I know its name (in Android)?
-
If you wanna do with **kotlin**, see: https://stackoverflow.com/a/52568692/1631967 – aminography Apr 15 '19 at 11:35
5 Answers
With something like this:
String mDrawableName = "myappicon";
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());

- 187,060
- 113
- 301
- 369

- 23,434
- 8
- 55
- 63
-
10Thanks, that helped me find a solution to a simliar problem! I'll use `getResources().getIdentifier(name, "id", getPackageName());` to get the ID of an ImageButton (as you would with R.id.name). – Select0r Jul 26 '10 at 19:19
-
44Note that the drawable name should NOT include an extension like ".png" – larham1 Aug 18 '11 at 18:17
-
1what exactly goes in the variable `name` ? I want to find the id of a button whose reference i know, in my case it is `button1` – John Watson Jul 26 '12 at 11:08
-
1`getResource()` and `getPackageName()` showing error. `cannot resolve method` – Srujan Barai Jul 22 '15 at 15:18
-
@Srujan Barai, `getResource()` and `getPackageName()` are methods from Activity. – The Berga May 02 '16 at 13:52
-
mDrawableName should be in **lower register**! Otherwise reID will be 0 – CatCap Dec 06 '17 at 13:20
-
Just make sure Resource Id is not 0 else it will throw Exception. The resource with this name must be present in the drawable folder. – Prince Oct 07 '20 at 11:55
You can also try this:
try {
Class res = R.drawable.class;
Field field = res.getField("drawableName");
int drawableId = field.getInt(null);
}
catch (Exception e) {
Log.e("MyTag", "Failure to get drawable id.", e);
}
I have copied this source codes from below URL. Based on tests done in this page, it is 5 times faster than getIdentifier(). I also found it more handy and easy to use. Hope it helps you as well.

- 9,825
- 16
- 72
- 145
-
2this might be faster but I found it can get you in trouble if you use proguard. This doesn't worked in proguard at least for me – Pedro Rainho Mar 13 '12 at 21:52
-
Catching generic exceptions is an especially bad idea. Furthermore, note that if there are no hard references to the drawable, Proguard may optimize the reference away as it doesn't believe that it's being used anywhere. – Paul Lammertsma Nov 24 '13 at 15:55
-
I understand now :) you mean that during optimization drawableName changes and resource cannot be found using this method, if i am right? :) – VSB Nov 25 '13 at 13:02
-
1@VSB: if your last comment is a response to Paul Lammertsma comment immediately above: No, he means that Proguard doesn't know this is a reference to the resource, and so might remove the resource, believing that it is not used. – ToolmakerSteve May 03 '15 at 14:24
-
once you use xxxxhdpi, xxhdpi, etc. this method becomes less useful – Someone Somewhere Jun 25 '16 at 14:08
-
-
I'm sorry - ignore my comment... it was my fault, this approach works well with various image sizes (xxxhdpi, etc) – Someone Somewhere Jun 25 '16 at 20:03
Example for a public system resource:
// this will get id for android.R.drawable.ic_dialog_alert
int id = Resources.getSystem().getIdentifier("ic_dialog_alert", "drawable", "android");
Another way is to refer the documentation for android.R.drawable class.

- 35,493
- 19
- 190
- 259
-
1
-
it means i confused by resources in the code and i use it in non activity class – Milaaaad Jan 19 '16 at 07:07
-
1@Milaaaad This method is for system resources. They don't depend on context. – naXa stands with Ukraine Aug 11 '16 at 09:09
You can use this function to get a Resource ID:
public static int getResourseId(Context context, String pVariableName, String pResourcename, String pPackageName) throws RuntimeException {
try {
return context.getResources().getIdentifier(pVariableName, pResourcename, pPackageName);
} catch (Exception e) {
throw new RuntimeException("Error getting Resource ID.", e)
}
}
So if you want to get a Drawable Resource ID, you can call the method like this:
getResourseId(MyActivity.this, "myIcon", "drawable", getPackageName());
(or from a fragment):
getResourseId(getActivity(), "myIcon", "drawable", getActivity().getPackageName());
For a String Resource ID you can call it like this:
getResourseId(getActivity(), "myAppName", "string", getActivity().getPackageName());
etc...
Careful: It throws a RuntimeException if it fails to find the Resource ID. Be sure to recover properly in production.

- 327
- 3
- 12

- 20,500
- 38
- 146
- 211
-
4I think you need to remove `static` from that, otherwise `getResources()` will not work. – Richard Le Mesurier May 09 '14 at 08:48
-
-
1@Leo.Han At the time I made that comment, the `Context` was not available. Fix was either to remove `static` and move it into a `Context` (e.g. `Activity` or `Application`); or alternate fix is what Jonathan did in his edit, which is to pass in a `Context` to the method. – Richard Le Mesurier Aug 26 '16 at 06:48
-
1@Richard Le Mesurier, I checked the edit history just now, it's just as what you said, thank you for your kindly reply after so many years :) – Veer Aug 26 '16 at 08:57
One other scenario which I encountered.
String imageName ="Hello" and then when it is passed into getIdentifier function as first argument, it will pass the name with string null termination and will always return zero. Pass this imageName.substring(0, imageName.length()-1)