How to get the selected index of a RadioGroup in Android How to get the selected index of a RadioGroup in Android xml xml

How to get the selected index of a RadioGroup in Android


You should be able to do something like this:

int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();View radioButton = radioButtonGroup.findViewById(radioButtonID);int idx = radioButtonGroup.indexOfChild(radioButton);

If the RadioGroup contains other Views (like a TextView) then the indexOfChild() method will return wrong index.

To get the selected RadioButton text on the RadioGroup:

 RadioButton r = (RadioButton) radioButtonGroup.getChildAt(idx); String selectedtext = r.getText().toString();


This should work,

int index = myRadioGroup.indexOfChild(findViewById(myRadioGroup.getCheckedRadioButtonId()));


You could have a reference to the radio group and use getCheckedRadioButtonId () to get the checked radio button id. Take a look here

RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radio_group);

Then when you need to get the selected radio option.

int checkedRadioButtonId = radioGroup.getCheckedRadioButtonId();if (checkedRadioButtonId == -1) {    // No item selected}else{    if (checkedRadioButtonId == R.id.radio_button1) {        // Do something with the button    }}