1

Working on an education android app. One provides answers through EditText input, radio buttons and CheckBox.

I want a check for the RadioGroup that when no RadioButton is clicked it will prompt the student and prevent submission. I succeeded in EditText but unsucessful for RadioButton.

public void checkEmptyButtons(View view){
  RadioGroup rg = (RadioGroup) findViewById(R.id.myRg);
    if (rg.getCheckedRadioButton()==-1{

    // this is where I have a problem
    //I don't even know how to search on the                     //dev.anderoid site
        }
    }

That's where am stucked. Thanks in advance .

AA Shakil
  • 538
  • 4
  • 14
Lovesome
  • 17
  • 6
  • Possible Duplicate of [How to check if a radioButton is checked in a radioGroup in Android](https://stackoverflow.com/questions/24992936/how-to-check-if-a-radiobutton-is-checked-in-a-radiogroup-in-android) – AA Shakil Jun 11 '18 at 11:40
  • Possible duplicate of [How to check if a radiobutton is checked in a radiogroup in Android?](https://stackoverflow.com/questions/24992936/how-to-check-if-a-radiobutton-is-checked-in-a-radiogroup-in-android) – AA Shakil Jun 11 '18 at 11:41

2 Answers2

1

you need radioGroup checked Change Listener:

rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
              //here you will get callback everytime radio button is checked along with id. You can have boolean checked here to know if any of your radio button is checked


            }
        });
Moulesh
  • 2,762
  • 1
  • 22
  • 32
0

Use getCheckedRadioButtonId() in stead of getCheckedRadioButton().

if (rg.getCheckedRadioButtonId() == -1) {
    //no radio buttons are checked
}
else {
    //any one of the radio buttons is checked
}
UkFLSUI
  • 5,509
  • 6
  • 32
  • 47