How to check which radiogroup button is selected?

Go To StackoverFlow.com

1

I have three radiobuttons in a radiogroup. How can I tell Java to do a different thing depending on which button is selected? I have the group and all of the buttons declared:

final RadioGroup size = (RadioGroup)findViewById(R.id.RGSize);
        final RadioButton small = (RadioButton)findViewById(R.id.RBS);
        final RadioButton medium = (RadioButton)findViewById(R.id.RBM);
        final RadioButton large = (RadioButton)findViewById(R.id.RBL);

I know I would say something like this:

if (size.getCheckedRadioButtonId().equals(small){

} else{

}

But equals is not the correct syntax... How can I ask java which button is selected?

2012-04-04 00:39
by Wilson
Please have a look at http://www.thetekblog.com/2010/07/android-radiobutton-in-radiogroup-example - Chetter Hummin 2012-04-04 00:47


1

Try:

if (size.getCheckedRadioButtonId() == small.getId()){
 ....
}
else if(size.getCheckedRadioButtonId() == medium.getId()){
 ....
}
2012-04-04 00:49
by znat


1

Because getCheckedRadioButtonId() returns an integer, you are trying to compare an integer with RadioButton object. You should compare the id of small(which is R.id.RBS) and getCheckedRadioButtonId():

switch(size.getCheckedRadioButtonId()){
    case R.id.RBS: //your code goes here..
                    break;
    case R.id.RBM: //your code goes here..
                    break;
    case R.id.RBL: //your code goes here..
                    break;
}
2012-04-04 00:49
by mtyurt


1

int selected = size.getCheckedRadioButtonId();

switch(selected){
case R.id.RBS:
   break;
case R.id.RBM:
   break;
case R.id.RBL:
   break;

}
2012-04-04 00:50
by Mark Pazon