How to check which radio button of a radio group is selected? [ANDROID]
Use following code.
RadioGroup rg = your_radio_group;
// It will return currently selected radio button id from RadioGroup
int id = rg.getCheckedRadioButtonId();
if(id == your_one_radio_button_id){
// First radio button is selected.
} else if(id == your_two_radio_button_id){
// Second radio button is selected.
}
This is working perfectly:
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);
int radioButtonID = radioGroup.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) radioGroup.findViewById(radioButtonID);
String selectedText = (String) radioButton.getText();
try this one, working for me for getting id and text both
radioGroup = findViewById(R.id.radio_group);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup rGroup, int checkedId) {
int radioButtonID = radioGroup.getCheckedRadioButtonId();
View radioButton = radioGroup.findViewById(radioButtonID);
int idx = radioGroup.indexOfChild(radioButton);
RadioButton r = (RadioButton) radioGroup.getChildAt(idx);
String selectedText = r.getText().toString();
System.out.println(idx);//For print Id
System.out.println(selectedText);//For print Text
}
});
Try to use this one its also working for me
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup rGroup, int checkedId) {
int radioBtnID = rGroup.getCheckedRadioButtonId();
View radioB = rGroup.findViewById(radioBtnID);
int position = group.indexOfChild(radioB);
}
});