45

If I get the image name as a variable like following:

var imageName = SERVICE.getImg();

Then, how can I get the resource with R.drawable.????, I tried R.drawable[imageName], but it failed. Any suggestions?

Leem
  • 17,220
  • 36
  • 109
  • 159
  • I dont use the NDK hence comment: getResources().getIdentifier (imageName, null , null); API: http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier(java.lang.String, java.lang.String, java.lang.String) – Blundell Jul 05 '11 at 14:03

5 Answers5

63
int id = getResources().getIdentifier(imageName, type, package);

This will get you the ID of the resource you are looking for. With it, you can then access the resource from the R class.

Using only the name parameter:

You can also include all the 3 info in the "name" parameter using the following format: "package:type/image_name", something like:

int id = getResources().getIdentifier("com.my.app:drawable/my_image", null, null);

This is useful when you're working with external components or libraries that you can't, or don't want to, change how getIdentifier() is called. e.g.: AOSP Launcher3

Perazzo
  • 1,127
  • 13
  • 24
nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
41

Try this:

int id = getResources().getIdentifier(imageName, "drawable", getPackageName());
Dark Magic
  • 498
  • 5
  • 8
  • 2
    As for `R.id.???` equivalent, it will be `int id = getResources().getIdentifier(imageName, "id", getPackageName());` – Neurotransmitter Jul 10 '17 at 20:41
  • It comes at a cost though, if you ever try to `Refactor -> Remove un-used resources`, it will delete the resource you dynamically access. So unless it's absolutely necessary, better not do it. – Irshu Aug 02 '18 at 06:31
  • Can confirm this works for me, although my code was ```int resourceId = context.getResources().getIdentifier(layoutName, "layout", context.getPackageName());``` – Joel Balmer Mar 03 '19 at 21:38
  • perfect answer.. Thank you ! – tenTen Feb 11 '21 at 18:39
15

You need reflection.

Suppose you have R.drawable.image1, if you wanna access it via the String name "image1", following should work:

String Name = "image1";
int id = R.drawable.class.getField(Name).getInt(null);

But notice it only get the Id of the image, you still need the inflater to get the actual drawable from it.

xandy
  • 27,357
  • 8
  • 59
  • 64
3

Use this function

public static String getResourceString(String name, Context context) {
    int nameResourceID = context.getResources().getIdentifier(name,
            "string", context.getApplicationInfo().packageName);
    if (nameResourceID == 0) {
        throw new IllegalArgumentException(
                "No resource string found with name " + name);
    } else {
        return context.getString(nameResourceID);
    }
}
0

You can use the getIdentifier method which will give you the resource id by its name. Check this thread for more details.

Pang
  • 9,564
  • 146
  • 81
  • 122
Mojo Risin
  • 8,136
  • 5
  • 45
  • 58
  • what is parameter "defPackage" in getIdentifier()??? Isn't it "projectName/res/drawable" ?? – Leem Jul 05 '11 at 14:31
  • defPackage - Optional default package to find, if "package:" is not included in the name. Can be null to require an explicit package. Your name should look something like this org.anddev.android.testproject:drawable/bug where bug is the drawable name and org.anddev.android.testproject is your main package name. Please read the article !! – Mojo Risin Jul 05 '11 at 14:43
  • Second link in answer is outdated. – Pang Jun 21 '23 at 01:04