Android get value of the selected radio button
Asked Answered
H

9

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.

Hinduism answered 25/6, 2012 at 17:46 Comment(0)
G
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();
}
Gilthead answered 25/6, 2012 at 17:51 Comment(6)
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 getChildAtEuell
What is radioGroup at int radioId = radioGroup.indexOfChild(radioButton);Wapiti
radioGroup is the object of RadioGroup; That group contains all the radio button. It is parent for those radio buttonArmanda
so rg1 and radioGroup is the same object in that case, right?Frisk
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();Inclinable
Couldn't you simply have done - (RadioButton) rg1.findViewById(id).getText(); ?Wappes
T
53

try this:

RadioGroup rg = (RadioGroup)findViewById(R.id.youradio);
String radiovalue = ((RadioButton)findViewById(rg.getCheckedRadioButtonId())).getText().toString();  
Theobald answered 25/6, 2012 at 17:52 Comment(2)
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 meHinduism
Thanks you. You should validade if it is null beucase may crash the app.Horsey
J
3
RadioGroup rg = (RadioGroup)findViewById(R.id.youradio);
String radiovalue = (RadioButton)this.findViewById(rg.getCheckedRadioButtonId())).getText().toString();  
Jhansi answered 2/5, 2014 at 12:36 Comment(0)
P
2

One Line Code

String buisnesstype = ((RadioButton) rdtranscompany.findViewById(rdtranscompany.getCheckedRadioButtonId())).getText().toString();
Pinzler answered 4/11, 2015 at 14:4 Comment(0)
A
1
rb1=(RadioButton)findViewById(rg1.getCheckedRadioButtonId());

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

Archenteron answered 28/2, 2017 at 20:11 Comment(0)
H
0

I think you should try this

RadioGroup rg=(RadioGroup)findViewById(R.id.youradio);
String radiovalue=(RadioButton)this.findViewById(rg.getCheckedRadioButtonId())).getText().toString();
Hoffer answered 5/2, 2015 at 14:7 Comment(0)
R
0
RadioGroup bhktype_RadioGr = (RadioGroup)findViewById(R.id.bhkypeRadioGroup);
int flatTypeId = bhktype_RadioGroup.getCheckedRadioButtonId();
String flat_type = ((RadioButton) findViewById(flatTypeId)).getText().toString();
Roa answered 28/4, 2015 at 6:1 Comment(0)
K
0

SImple answer one line

View v = yourView;  // as a button

String radiovalue = (RadioButton)v).getText().toString();
Kehr answered 3/5, 2016 at 16:9 Comment(0)
M
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
}
Melquist answered 10/4, 2022 at 12:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.