How to access resource with dynamic name in my case?

26,045

Solution 1

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

Solution 2

Try this:

int id = getResources().getIdentifier(imageName, "drawable", getPackageName());

Solution 3

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.

Solution 4

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);
    }
}
Share:
26,045

Related videos on Youtube

Leem
Author by

Leem

Updated on August 11, 2020

Comments

  • Leem
    Leem almost 4 years

    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
    Leem almost 13 years
    @nicholas, I got resource not found exception. If I have a image named "icon1.gif" in folder "drawable", isn't it getIdentifier("icon1", "gif", "drawable") ??
  • nicholas.hauschild
    nicholas.hauschild almost 13 years
    Did you look at the link? I think it would be ("icon1", "drawable", "the.containing.package")
  • Leem
    Leem almost 13 years
    @nicholas, what does the.containing.package mean??? Isn't the containing package = "projectName/res/drawable" ??
  • Leem
    Leem almost 13 years
    what is parameter "defPackage" in getIdentifier()??? Isn't it "projectName/res/drawable" ??
  • nicholas.hauschild
    nicholas.hauschild almost 13 years
    @Leem: the package is your projects package.
  • Mojo Risin
    Mojo Risin almost 13 years
    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 !!
  • Joshua Pinter
    Joshua Pinter over 10 years
    Why does this work when the other getIdentifier() methods fail?
  • Neurotransmitter
    Neurotransmitter almost 7 years
    As for R.id.??? equivalent, it will be int id = getResources().getIdentifier(imageName, "id", getPackageName());
  • Koushik Shom Choudhury
    Koushik Shom Choudhury almost 6 years
    make sure to leave the extension (.png or .jpg) out of the imageName String, else resourceId will be set to 0x0 integer which refers to No such resources found
  • Irshu
    Irshu almost 6 years
    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.
  • Joel Balmer
    Joel Balmer over 5 years
    Can confirm this works for me, although my code was int resourceId = context.getResources().getIdentifier(layoutName, "layout", context.getPackageName());
  • tenTen
    tenTen over 3 years
    perfect answer.. Thank you !