Question

I'm Developing an android app in which the Questionnaire activity contains Questions which as radio Buttons and also a Button(Next).So when the button is pressed I've to check whether all the Questions are answered.if any of the Question is not answered then a alert message should pop up stating that the particular Question no is not answered.Can anyone please help me with the java code. Thanks in Advance. Here is the view of my Questionnaire activity

Here is the Java code. I've commented on the line where I'm getting an error.

public class ManagerQuestionnaire1 extends Activity
{

 RadioButton rb1;
 RadioButton rb2;
 RadioButton rb3;
 RadioButton rb4;
 RadioButton rb5;
 RadioButton rb6;
 RadioButton rb7;
 RadioButton rb8;
 RadioButton rb9;
 RadioGroup rg1;
 RadioGroup rg2;
 RadioGroup rg3;
 Button next;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_manager_questionnaire1);
    addButtonListener();
    }
public void addButtonListener()
{
 rb1=(RadioButton)findViewById(R.id.radioButton1);
 rb2=(RadioButton)findViewById(R.id.radioButton2);
 rb3=(RadioButton)findViewById(R.id.radioButton3);
 rb4=(RadioButton)findViewById(R.id.radioButton4);
 rb5=(RadioButton)findViewById(R.id.radioButton5);
 rb6=(RadioButton)findViewById(R.id.radioButton6);
 rb7=(RadioButton)findViewById(R.id.radioButton7);
 rb8=(RadioButton)findViewById(R.id.radioButton8);
 rb9=(RadioButton)findViewById(R.id.radioButton9);
 rg1=(RadioGroup)findViewById(R.id.Mquestion1);
 rg2=(RadioGroup)findViewById(R.id.Mquestion2);
 rg3=(RadioGroup)findViewById(R.id.Mquestion3);
    Button next=(Button)findViewById(R.id.button1);
 next.setOnClickListener(new OnClickListener() {

     @Override
     public void onClick(View v)
     {
         if(validationSuccess()){
             Intent intent = new Intent(ManagerQuestionnaire1.this, ManagerQuestionnaire2.class);
             startActivity(intent);
         }
     }
 });
}

 private Boolean validationSuccess()
 {
if(rg1.getCheckedRadioButtonId()==-1&&rg2.getCheckedRadioButtonId()==-1&&rg3.getCheckedRadioButtonId()==-1)
{
    alertDialog();
    return false;
}
 if(rb1.isChecked()==false&rb2.isChecked()==false&&rb3.isChecked()==false)
 {
     alertDialog();
     return false;
     }
 if(rb4.isChecked()==false&&rb5.isChecked()==false&&rb6.isChecked()==false)
 {
     alertDialog();
     return false;
     }
 if(rb7.isChecked()==false&&rb8.isChecked()==false&&rb9.isChecked()==false)
 {
     alertDialog();
     return false;
     } 
 return true;
  }
  private void alertDialog()
{
  AlertDialog alert= new AlertDialog.Builder(ManagerQuestionnaire1.this).create();
  alert.setTitle("Exception:Complete the Questions");
  alert.setMessage("Please ensure all Questions are answered");      
 } 
Was it helpful?

Solution

You may also use below code:

       next.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if(validationSuccess()){
                            Intent intent = new Intent(ManagerQuestionnaire1.this, ManagerQuestionnaire2.class);
                            startActivity(intent);
                        }
                    }
                });

        private Boolean validationSuccess(){
    if(rg1.getCheckedRadioButtonId()==-1&&rg2.getCheckedRadioButtonId()==-1&&rg3.getCheckedRadioButtonId()==-1){
    alertDialog();
    return false;
    }


//optional to add whether to check which questn is not answered   

        if(mBtn1.isChecked()==false&&mBtn2.isChecked()==false&&mBtn3.isChecked()==false){
                    alertDialog();
                    return false;
                    }
                if(mBtn4.isChecked()==false&&mBtn5.isChecked()==false&&mBtn6.isChecked()==false){
                    alertDialog();
                    return false;
                    }
                if(mBtn7.isChecked()==false&&mBtn8.isChecked()==false&&mBtn9.isChecked()==false){
                    alertDialog();
                    return false;
                    }
    return true;    
            }




    private void alertDialog() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            ManagerQuestionnaire1.this);
    alertDialogBuilder.setMessage("Please ensure all Questions are answered")
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

        AlertDialog alert = alertDialogBuilder.create();
        alert.show();


                }

where mBtn1,mBtn2..are your radioButton's

OTHER TIPS

You can get the id of the checked RadioButton in a particular RadioGroup by:

int selectedId = radioGroup.getCheckedRadioButtonId();

You can try something like below.

public void onClick(View v) {    

        int checked = rg1.getCheckedRadioButtonId();

        switch(checked)
        {
        case R.id.always:
        Toast.makeText(ManagerQuestionnaire1 .this, "First is selected", Toast.LENGTH_SHORT).show();
        break;
        case R.id.sometime:
        Toast.makeText(ManagerQuestionnaire1 .this, "Second is selected", Toast.LENGTH_SHORT).show();
        break;
        case R.id.notatall:
        Toast.makeText(ManagerQuestionnaire1 .this, "Third is selected", Toast.LENGTH_SHORT).show();
        break;
        default:
        Toast.makeText(ManagerQuestionnaire1 .this, "pleas check any button", Toast.LENGTH_SHORT).show();
        break;
        }
}

If you want to show Alert dialog instead of toast then you can replace it with alert dialog. Like wise same for your rg2 and rg3 you can check.

you must be having multiple radiogroups on the screen.

Here is the example to check whether any all questions are attempted or not in your case

int radioGroupIds = new int []{R.id.rg1, R.id.rg2, r.id.rg3};

for(int rg : radioGroupIds)
{

int selectedAns = (RadioGroup)findViewById(rg).getCheckedRadioButtonId();

// Returns the identifier of the selected radio button in this group. Upon empty selection, the returned value is -1.

if(selectedAns == -1)
{
      // TODO answer is not selected
      // This represents that there is missing answer of any question
}else
{
      // TODO answer is selected
      // This represents that answer is selected for question
}

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top