How to get Resource Name from Resource id

Android

Android Problem Overview



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 ?

Android Solutions


Solution 1 - Android

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)

Solution 2 - Android

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%.

Solution 3 - Android

Kotlin:

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

Solution 4 - Android

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

Solution 5 - Android

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.. :) )

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCode_LifeView Question on Stackoverflow
Solution 1 - AndroidShubhayuView Answer on Stackoverflow
Solution 2 - AndroidAnil JadhavView Answer on Stackoverflow
Solution 3 - AndroidAnand MakwanaView Answer on Stackoverflow
Solution 4 - Androiduser6751090View Answer on Stackoverflow
Solution 5 - AndroidDimitris MakrisView Answer on Stackoverflow