197


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 ?

ColdFire
  • 6,764
  • 6
  • 35
  • 51
Code_Life
  • 5,742
  • 4
  • 29
  • 49
  • 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 . – Code_Life Apr 13 '12 at 08:53

5 Answers5

437

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)
Code_Life
  • 5,742
  • 4
  • 29
  • 49
Shubhayu
  • 13,402
  • 5
  • 33
  • 30
  • 5
    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. – Steve Sep 05 '12 at 12:03
  • 8
    Any idea what the difference is between `getResourceEntryName(int resid)` and `getResourceName(int resid)`? – Joshua Pinter Jan 22 '14 at 17:10
  • 10
    @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** – Jose_GD Mar 22 '14 at 01:26
  • @Shubhayu - how might you do this in an espresso test? getResources() function doesn't seem to be available! – 1ak31sha Oct 05 '16 at 17:07
  • I wouldn't recommend use it if you use Proguard (`minifyEnabled true`), especially for `getResources().getIdentifier(...` – user25 Jul 14 '18 at 22:54
  • What is `v` in the Kotlin example? – Kenny Sexton May 26 '21 at 12:16
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%.

Anil Jadhav
  • 2,128
  • 1
  • 17
  • 31
  • 3
    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. – Steve Sep 05 '12 at 12:01
5

Kotlin:

val name = v.context.resources.getResourceEntryName(v.id)
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Anand Makwana
  • 51
  • 1
  • 1
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

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

Dimitris Makris
  • 5,183
  • 2
  • 34
  • 54
  • No, u got it all wrong . I want to retrieve text **radio1** from **android:id="@+id/radio1"** – Code_Life Apr 13 '12 at 08:50
  • 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? – Dimitris Makris Apr 13 '12 at 08:52
  • @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 – Code_Life Apr 13 '12 at 08:53
  • 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. – Dimitris Makris Apr 13 '12 at 08:57
  • 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 . – Code_Life Apr 13 '12 at 09:03