27

I have a RadioGroup rg1 and I want to get the value of the selected radio button.

I know that I can get the id of the selected radio button by using:

if(rg1.getCheckedRadioButtonId()!=-1)
int id= rg1.getCheckedRadioButtonId()

that gives me the id , but I want the value of that button.

deathstroke
  • 526
  • 12
  • 33
Totti
  • 665
  • 5
  • 12
  • 26

9 Answers9

65

You need to get the radio button at that index, then get the value of the text of that button. Try this code below.

if(rg1.getCheckedRadioButtonId()!=-1){
    int id= rg1.getCheckedRadioButtonId();
    View radioButton = rg1.findViewById(id);
    int radioId = radioGroup.indexOfChild(radioButton);
    RadioButton btn = (RadioButton) rg1.getChildAt(radioId);
    String selection = (String) btn.getText();
}
Otra
  • 8,108
  • 3
  • 34
  • 49
  • 4
    Is the id returned from getCheckedRadioButton only the id within the RadioGroup? Why couldn't you get the id of the button and then use a more general findViewById to get the RadioButton instad of adding extra steps of indexOfChild and getChildAt – Rarw May 28 '13 at 18:52
  • 1
    What is radioGroup at int radioId = radioGroup.indexOfChild(radioButton); – Mitesh Shah May 06 '14 at 13:24
  • radioGroup is the object of RadioGroup; That group contains all the radio button. It is parent for those radio button – Simon Chius Apr 21 '15 at 10:17
  • so rg1 and radioGroup is the same object in that case, right? – Jack Lynx Aug 23 '16 at 15:28
  • Or you can get the radio button with their ID's and get its text if its checked using `isChecked` method. Like this `RadioButton radio_male = ((RadioButton) findViewById(R.id.rbtn_male)); String gend = radio_male.isChecked() ? radio_male.getText().toString() : radio_female.getText().toString();` – vikas devde Dec 26 '17 at 15:13
  • Couldn't you simply have done - `(RadioButton) rg1.findViewById(id).getText();` ? – Aman Grover Apr 19 '20 at 18:36
53

try this:

RadioGroup rg = (RadioGroup)findViewById(R.id.youradio);
String radiovalue = ((RadioButton)findViewById(rg.getCheckedRadioButtonId())).getText().toString();  
JulienGenoud
  • 592
  • 9
  • 21
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • thank you , i have got an answer already, hope i could select more than one answer as accepted answers :( , really thank you man, you always help me – Totti Jun 25 '12 at 17:57
  • 1
    Thanks you. You should validade if it is null beucase may crash the app. – pess0a Apr 14 '17 at 13:41
3
RadioGroup rg = (RadioGroup)findViewById(R.id.youradio);
String radiovalue = (RadioButton)this.findViewById(rg.getCheckedRadioButtonId())).getText().toString();  
Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
prawins
  • 31
  • 1
2

One Line Code

String buisnesstype = ((RadioButton) rdtranscompany.findViewById(rdtranscompany.getCheckedRadioButtonId())).getText().toString();
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
1
rb1=(RadioButton)findViewById(rg1.getCheckedRadioButtonId());

Now you can use rb1.getText() to get the text on the Radiobutton that is checked

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
karthik gorijavolu
  • 860
  • 3
  • 14
  • 28
0

I think you should try this

RadioGroup rg=(RadioGroup)findViewById(R.id.youradio);
String radiovalue=(RadioButton)this.findViewById(rg.getCheckedRadioButtonId())).getText().toString();
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Faruk Nasir
  • 192
  • 1
  • 6
0
RadioGroup bhktype_RadioGr = (RadioGroup)findViewById(R.id.bhkypeRadioGroup);
int flatTypeId = bhktype_RadioGroup.getCheckedRadioButtonId();
String flat_type = ((RadioButton) findViewById(flatTypeId)).getText().toString();
Ganesh Jogam
  • 3,117
  • 1
  • 13
  • 10
0

SImple answer one line

View v = yourView;  // as a button

String radiovalue = (RadioButton)v).getText().toString();
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
vincent
  • 1
  • 1
0

Get the radio button text only when a radio button is checked in a radio group by this Kotlin code -

radioGroup.setOnCheckedChangeListener { rg, i ->
    val selectedId = radioGroup.checkedRadioButtonId
    val radioButton = findViewById<RadioButton>(selectedId)
    myTextView.text = radioButton.text
}