How to get Resource Name from Resource id
AndroidAndroid 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:
-
to get string like
radio1
:getResources().getResourceEntryName(int resid);
-
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.. :) )