getCheckedRadioButtonId() returning useless int?
getCheckedRadioButtonId()
returns the id
of the RadioButton
(or -1
if no RadioButtons
are checked) that is checked in the Radiogroup
. If you set distinct ids to the RadioButons
in the layout then you will try to match those ids with the return of the method to see which one is checked:
//field in the class
private static final int RB1_ID = 1000;//first radio button id
private static final int RB2_ID = 1001;//second radio button id
private static final int RB3_ID = 1002;//third radio button id
//create the RadioButton
RadioButton rb1 = new RadioButton(this);
//set an id
rb1.setId(RB1_ID);
int btn = radioGroup.getCheckedRadioButtonId();
switch (btn) {
case RB1_ID:
// the first RadioButton is checked.
break;
//other checks for the other RadioButtons ids from the RadioGroup
case -1:
// no RadioButton is checked inthe Radiogroup
break;
}
store the checked ID, then compare it to each button using the function radioButton.getID() using a switch statement or if-else chains