Home » Android » android – How to get Resource Name from Resource id

android – How to get Resource Name from Resource id

Posted by: admin March 10, 2020 Leave a comment

Questions:

In my layout I have defined something like this .

<RadioButton
    android:id="@+id/radio1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Dnt want this text" />

Assume that some function in activity returns me this id (id of radioButton). Now i want to get this text radio1 from this id.
In short I want to retrieve text radio1 written in android:id="@+id/radio1"

Can somebody tell me how is it possible ?

How to&Answers:

In your Activity, try these:

  1. to get string like radio1:

    getResources().getResourceEntryName(int resid);
    
  2. to get string like com.sample.app:id/radio1:

    getResources().getResourceName(int resid);
    

In Kotlin Now :

val name = v.context.resources.getResourceEntryName(v.id)

Answer:

You have id(‘long’ type) from that id you want to access radio button id(name) that is radio1.
You use this

getResources().getResourceEntryName(id);

in using above you can get name of radio button i.e. radio1. here parameter id is which you have(long type).
Try this it will helps you 100%.

Answer:

Kotlin:

val name = v.context.resources.getResourceEntryName(v.id)

Answer:

If I am right, what you wanted to retrieve is the word “radio1” (from the id itself?) so if that’s the case then first you need to get its id.

int intname= buttonname.getId();

then get the result of it

String stringname= getResources().getResourceEntryName(intname);

hoped I helped

Answer:

You mean you want to take the string text of the id?

Since you have defined it you should know what this is.

If you have a layout and you want to find if a View has a specific id, you can traverse the whole layout and check with getId(), if the id of each View is the id you are looking for..

Hope this helps (if I have understand correct your question.. 🙂 )