Android: Radiobutton non checked

607 Views Asked by At

I want to do an Android App Quiz. I did a radiogroup with 4 radiobuttons that represent the 4 answers. I want that when these radiobuttons aren't clicked the app shows an error, but my doesn't do it. This my code:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        RadioGroup champ=(RadioGroup)findViewById(R.id.answer1);


        champ.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @SuppressWarnings("null")
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    // TODO Auto-generated method stub

                    switch(checkedId) {

                    case R.id.answer1A:
                        ans1 =0;
                        break;
                    case R.id.answer1B:
                        ans1 =1;
                        break;
                    case R.id.answer1C:
                        ans1 =2;
                        break;
                    case R.id.answer1D:
                        ans1 =3;
                        break;
                    case R.id.answer1E:
                        ans1 =4;
                        break;
                    default:
                        ans1=(Integer) null;
                        Toast.makeText(MainActivity.this, "error",  Toast.LENGTH_LONG).show();
                        break;

                    }
                }
            });

Why?Can you help me, please?

2

There are 2 best solutions below

1
On

First assign:

ans1=-1; //in onCreate or at the time of declaration

You might be having a button or something clicking on which you would be accessing which answer was checked, there you can check :

if(ans1==-1)
{
   //show error message
}
else
{
  //Success
} 
1
On

This check is not executing because this is a checkedchangelistener which will trigger when you click on some radiobutton (when status is changed).

All you have to do is Check the status of the each and every radiobutton outside this listener.