203


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 ?

1
  • Its a long story .. i need to populated data into a large no of editText from some data received from server . So instead of manually setting data to all textbox , i am writing a method that could this work for me .. for that i need this . Commented Apr 13, 2012 at 8:53

5 Answers 5

447

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) 
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for answering this, I spent the better part of an hour googling this problem - the dev site kept pointing me to getString() - which was bloody useless for this.
Any idea what the difference is between getResourceEntryName(int resid) and getResourceName(int resid)?
@JoshPinter the reference says: getResourceEntryName: Return the entry name for a given resource identifier getResourceName: Return the full name for a given resource identifier. This name is a single string of the form "package:type/entry" The difference then seems to be in the added package:type for getResourceName
@Shubhayu - how might you do this in an espresso test? getResources() function doesn't seem to be available!
I wouldn't recommend use it if you use Proguard (minifyEnabled true), especially for getResources().getIdentifier(...
|
12

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

1 Comment

Thank you. Glad somebody actually answered the question instead of the usual "why do you need that [noob]" responses. Having the name of the element while I am iterating through them is INVALUABLE for debugging. I mean, really, how am I going to find the TextView with id 354814715? (which if I'm not mistaken isn't even constant between compilations) - not sure why the OP needed it, but I needed it to verify my font-scaling function was actually iterating though all sub-elements.
5

Kotlin:

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

3 Comments

Maybe you can add more information on what problem this solves and how it does so :)
@PhilippMeissner it is all obvious
Let's agree to disagree.
1

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

Comments

-2

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

5 Comments

No, u got it all wrong . I want to retrieve text radio1 from android:id="@+id/radio1"
Text radio1 IS the id of your View. It is not a simple text that you can get, split, etc.. In order to better help you, can you tell us what exactly do you need this text for?
@DimitrisMakris :Its a long story .. i need to populated data into a large no of editText from some data received from server . So instead of manually setting data to all textbox , i am writing a method that could this work for me .. for that i need this
So if you have a response of 10 for example strings with which you want to populate 10 radiobuttons, I think a solution could be to dynamically create these radiobuttons and set their value. Another approach could be to use a ListView with radiobuttons as list items, if ofcourse this fits your needs regarding the layout.
Thanks for your respose . But u got it all wrong i have 10 editBox and i receive as json string with the value to be filled in these 10 editbox after this string is parserd it is converted into HashMap. Now instead manually mapping each value (corresponding to key of hashmap) to editText text.. i am writting a method that could this work for me .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.