1

I'm having two RadioGroups in one layout and each radiogroup has three radio buttons. so how can i use if else statement.

[two radiogroups in each group having three radiobuttons.when user clicked on one radiobutton then it will display correct answer.else wrong answer.][1]

Anil Meenugu
  • 1,411
  • 1
  • 11
  • 16
user3736680
  • 31
  • 1
  • 6
  • for example radiobuttons is used in the quiz application. – user3736680 Jun 26 '15 at 09:01
  • for example english subject having 10 questions each question having three radio buttons – user3736680 Jun 26 '15 at 09:02
  • possible duplicate of [How to check if a radiobutton is checked in a radiogroup in Android?](http://stackoverflow.com/questions/24992936/how-to-check-if-a-radiobutton-is-checked-in-a-radiogroup-in-android) – Narendra Singh Jun 26 '15 at 09:11

3 Answers3

1

Why do you want to use if else statement. You can do it in the easy way.

 int radioButtonId = rbGroup.getCheckedRadioButtonId();
 View radioButton = radioButtonGroup.findViewById(radioButtonId);
Emil
  • 2,786
  • 20
  • 24
0

You can do like this to get the checked radiobutton from the radioGroup

RadioGroup g = (RadioGroup) findViewById(R.id.rBtnDigits);

switch (g.getCheckedRadioButtonId())
{
case R.id.rbtnButton1
//do something
break;

case R.id.rbtnButton2
//do something
break;
}
Narendra Singh
  • 3,990
  • 5
  • 37
  • 78
-2
rg = (RadioGroup) findViewById(R.id.radioGroup1);
    button = (RadioButton) findViewById(R.id.radio0);

    rg.performClick();
    rg.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

 public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub

            @SuppressWarnings("unused")
            int selected = rg.getCheckedRadioButtonId();

            pos = rg.indexOfChild(findViewById(rg.getCheckedRadioButtonId()));

            switch (pos) {
            case 0:
                Toast.makeText(getBaseContext(),
                        "You have Clicked RadioButton 1",
                        Toast.LENGTH_SHORT).show();
                break;
            case 1:
                Toast.makeText(getBaseContext(),
                        "You have Clicked RadioButton 2",
                        Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(getBaseContext(),
                        "You have Clicked RadioButton 3",
                        Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(getBaseContext(),
                        "You have Clicked RadioButton 4",
                        Toast.LENGTH_SHORT).show();
                break;

            }
        }

    });
Chaudhary Amar
  • 836
  • 8
  • 20