0

I'm in a situation where I have four radiobutton, next button and one question. now my issue is that if the user clicks next button without selecting any option it should show a toast message and if user clicks any one of the radio button, it should be checked whether it is right or wrong. I have partially implemented but I cant get the exact solution. any one help me with this.

Thanks in advance

I have posted my code below

Next.setOnClickListener(new View.OnClickListener() 
     {
         @Override
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub



                count++;

                if(viewFlipper.getDisplayedChild()==0)
                {
                    id1 = rag1.getCheckedRadioButtonId();
                    rab1=(RadioButton)findViewById(id1);
                    //System.out.println("1nd question answer"+rab1.getText());

                    if (rag1.getCheckedRadioButtonId()!=R.id.btn1_1||rag1.getCheckedRadioButtonId()!=R.id.btn1_2||rag1.getCheckedRadioButtonId()!=R.id.btn1_3||rag1.getCheckedRadioButtonId()!=R.id.btn1_4||rag1.getCheckedRadioButtonId()!=R.id.btn1_5&&rag1.getCheckedRadioButtonId()==-1)
                    {
                        Toast.makeText(getApplicationContext(), "Please enter the  answer", Toast.LENGTH_SHORT).show();
                    }



                     if((rag1.getCheckedRadioButtonId()!=-1)&&rab1.getText().equals(c_alcorrectanswer.get(0)))
                     {
                         System.out.println("1nd question answer"+rab1.getText());
                     }

                    else
                    {
                        Toast.makeText(getApplicationContext(), "Please enter the correct answer", Toast.LENGTH_SHORT).show();
                        viewFlipper.stopFlipping();
                    }



                }
Somasundaram NP
  • 1,018
  • 1
  • 12
  • 36

1 Answers1

1

You should check within one condition only. Whether any of your RadioButton of your RadioGroup is selected or not.

  int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
  if(radioButtonID > 0) //return -1 if none of radiobutton is selected from radiogroup
   {
     //move to next
   }
  else
  {
   // show toast. Please select any option.
  }

As per your code snippet:

if(viewFlipper.getDisplayedChild()==0)
{
 id1 = rag1.getCheckedRadioButtonId();
    if(id1 < 0) //return -1 if none of radio button is selected
     {
     //Toast. Please select any answer
     }
 }

developer.android.com documentation reference

Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
  • @Somasundaram Have look in my edited answer. Except checking so many `or` `and`. You can use simple one condition to know whether any radio button is selected or not. – Vikalp Patel Apr 05 '14 at 11:59
  • sorry for delay I was looking for solution, thing is that I have four layout in view flipper and I get the current child id based on that I'm performing this function. I will check your edited code... – Somasundaram NP Apr 05 '14 at 12:35