Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android get value of the selected radio button

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.

like image 653
Totti Avatar asked Jun 25 '12 17:06

Totti


People also ask

How do you programmatically determine whether a RadioButton is checked in Android?

You can check the current state of a radio button programmatically by using isChecked() method. This method returns a Boolean value either true or false. if it is checked then returns true otherwise returns false.

What does getCheckedRadioButtonId return?

getCheckedRadioButtonId() will return unknown value.


2 Answers

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(); } 
like image 143
Otra Avatar answered Oct 08 '22 16:10

Otra


try this:

RadioGroup rg = (RadioGroup)findViewById(R.id.youradio); String radiovalue = ((RadioButton)findViewById(rg.getCheckedRadioButtonId())).getText().toString();   
like image 39
ρяσѕρєя K Avatar answered Oct 08 '22 15:10

ρяσѕρєя K