Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8645065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:27:12+00:00 2026-06-12T12:27:12+00:00

I have a custom folder, file and custom XML resource class in my res

  • 0

I have a custom folder, file and custom XML resource class in my “res” folder.

I create some custom objects, which I call:

<area id="@+id/someId" name="Some Name" />

I can access them statically by R.id.someId.

However, I need to get the resource id at runtime and I need to do that by the “name”. In other words, I display that “Some Name” in the list and I need to get the id of knowing that the user selected “Some Name” from the ListView. (I am NOT looking for the id of the ListItem, I actually want to search my resources and get the id of the area xml object)

For example:

I would like to do something of the following:

int id = getIdFromResourceName("Some Name"); 

Is this possible?

I have tried using:

int i = this.getResources().getIdentifier("Some Name", "area", this.getPackageName());

…but that did not seem to work. I always get 0.

EDIT

As suggested below by Geobits, is there a way to load all of the resources from a res file and save them in an array/map, such as Map<id,name> so I can search them later?

Thank you for the help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:27:13+00:00Added an answer on June 12, 2026 at 12:27 pm

    I am not sure if this is what you need. But here is my solution proposal. This is how I would do it, if your resource was a drawable:

        public int findResourceIdByName(String name) {
            Field[] fields = R.drawable.class.getFields();  // get all drawables
            try {
                for(int i=0; i<fields.length; i++) {        // loop through all drawable resources in R.drawable
                    int curResId = fields[i].getInt(R.drawable.class); // Returns the value of the field in the specified object as an int.
                                                                      //This reproduces the effect of object.fieldName
    
                    Drawable drawable = getResources().getDrawable(R.drawable.icon); // get the Drawable object
                    if(drawable.getName().equals(name)) {   //getName() is NOT possible for drawable, this is just an example
                        return curResId;                    // return the corresponding resourceId
                                                            // or you could return the drawable object instead, 
                                                            // depending on what you need.
                    }
                }
    
                return -1; // no ResourceId found for this name
    
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    

    This is using Reflection, so it won’t be the most efficient approach. If you call this method frequently, you might need to cache the result of

    Field[] fields = R.drawable.class.getFields();
    

    at least.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have built a custom class, which i call from a frame script. the
I have created a custom xml file which I use as a config for
I have some custom made XML files and a read me file that I've
I have a custom jar file in the tomcat lib/ folder. As per my
I want to add one xml file (which has some static data related to
So i have this listview with my custom XML file that i made for
I created Area called Admin. I have Custom Helper in the root folder called
In order to create custom buttons in Android I create a XML file where
I have custom Rythm template tag (sayHi.html) which I want to call from parent
I have a custom container folder. I named it .llp . It is actually

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.