Question

I'm Developing an android app in which the Questionnaire activity contains Questions which as radio Buttons and also a Button.So when the button is pressed I've to check whether all the Questions are answered.if 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.

This is my Questionnaire with radio buttons and a button placed below

<RadioGroup
    android:id="@+id/Mquestion1"  
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
<RadioButton
    android:id="@+id/radioButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_1_rb1" />

<RadioButton
    android:id="@+id/radioButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_1_rb2" />

<RadioButton
    android:id="@+id/radioButton3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_1_rb3" />
</RadioGroup>
<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_2_view"
    android:textAppearance="?android:attr/textAppearanceSmall" />
<RadioGroup
android:id="@+id/Mquestion2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
    android:id="@+id/radioButton4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_2_rb1" />

<RadioButton
    android:id="@+id/radioButton5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_2_rb2" />

<RadioButton
    android:id="@+id/radioButton6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_2_rb3" />
</RadioGroup>
<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_3_view"
    android:textAppearance="?android:attr/textAppearanceSmall" />
<RadioGroup
android:id="@+id/Mquestion3"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
    android:id="@+id/radioButton7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_3_rb1" />

<RadioButton
    android:id="@+id/radioButton8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_3_rb2" />

<RadioButton
    android:id="@+id/radioButton9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/MQ1_3_rb3" />
</RadioGroup>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="44dp" >

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:onClick="sendMessage"
        android:text="@string/MQ1_next" />

</RelativeLayout>

</LinearLayout>

and here is the java code

public class ManagerQuestionnaire1 extends Activity
{

Button next;
RadioGroup rg1;
RadioGroup rg2;
RadioGroup rg3;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_manager_questionnaire1);
    final RadioGroup    rg1=(RadioGroup)findViewById(R.id.Mquestion1);
     final RadioGroup   rg2=(RadioGroup)findViewById(R.id.Mquestion2);
     final RadioGroup   rg3=(RadioGroup)findViewById(R.id.Mquestion3);
    Button next=(Button)findViewById(R.id.button1);
            next.setOnClickListener(new View.OnClickListener()
            {
                public void OnClick(View v) //Here in this line I'm getting a caution symbol which says REMOVE METHOD 'OnClick'
                {

                    if((rg1.getCheckedRadioButtonId()!=R.id.radioButton1 || rg1.getCheckedRadioButtonId()!=R.id.radioButton2 || rg1.getCheckedRadioButtonId()!=R.id.radioButton3)||(rg2.getCheckedRadioButtonId()!=R.id.radioButton4 || rg2.getCheckedRadioButtonId()!=R.id.radioButton5 || rg2.getCheckedRadioButtonId()!=R.id.radioButton6)||(rg3.getCheckedRadioButtonId()!=R.id.radioButton7 || rg3.getCheckedRadioButtonId()!=R.id.radioButton8 || rg3.getCheckedRadioButtonId()!=R.id.radioButton9))
                    {
                         AlertDialog alert= new AlertDialog.Builder(ManagerQuestionnaire1.this).create();
                         alert.setTitle("Exception:Complete the Questions");
                         alert.setMessage("Please ensure all Questions are answered");
                    }
                    else    
                    {
                         Intent intent = new Intent(ManagerQuestionnaire1.this, ManagerQuestionnaire2.class);
                            startActivity(intent);
                    }
                }

                @Override
                public void onClick(View v) 
                {
                    // TODO Auto-generated method stub

                }


    });
}
Was it helpful?

Solution

You should check on the SelectedID of the radioGroup and check for the return ID and their corresponding ids as following:

0)Declare all the used UI component in the global scoop of your code as following:

Class x extends Activity{
    RadioButton radioButton1;
    ...

1)Initialize all the XML buttons to get each button ID in OnCreate as following

radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
//The rest of other buttons
....
....

2)In the clickListener

next.setOnClickListener(new View.OnClickListener()
{
 public void OnClick(View v)
{
 if(Mquestion1.value!=radioButton1 || Mquestion1.value!=radioButton2 || Mquestion1.value!=radioButton3 || Mquestion1.value!=radioButton4) ||(Mquestion2.valu!= radioButton5 || ..... )||(Mquestion3.value....)
{
      //Show the allert
}
}
});

Replace the dots with your rest checks.

Use the following code instead of yours

Button next;
    RadioGroup rg1;
    RadioGroup rg2;
    RadioGroup rg3;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_manager_questionnaire1);
    rg1=(RadioGroup)findViewById(R.id.Mquestion1);
    rg2=(RadioGroup)findViewById(R.id.Mquestion2);
    rg3=(RadioGroup)findViewById(R.id.Mquestion3);
    next=(Button)findViewById(R.id.button1);
    next.setOnClickListener(new View.OnClickListener()
    {
        public void OnClick(View v)
        {
            if((rg1.getCheckedRadioButtonId()!=R.id.radioButton1 || Mquestion1.getCheckedRadioButtonId()!=R.id.radioButton2 || Mquestion1.getCheckedRadioButtonId()!=R.id.radioButton3)||(Mquestion2.getCheckedRadioButtonId()!=R.id.radioButton4 || Mquestion2.getCheckedRadioButtonId()!=R.id.radioButton5 || Mquestion2.getCheckedRadioButtonId()!=R.id.radioButton6)||(Mquestion3.getCheckedRadioButtonId()!=R.id.radioButton7 || Mquestion3.getCheckedRadioButtonId()!=R.id.radioButton8 || Mquestion3.getCheckedRadioButtonId()!=R.id.radioButton9))
            {
                 AlertDialog alert= new AlertDialog.Builder(ManagerQuestionnaire1.this).create();
                 alert.setTitle("Exception:Complete the Questions");
                 alert.setMessage("Please ensure all Questions are answered");
            }
            else    
            {
                 Intent intent = new Intent(ManagerQuestionnaire1.this, ManagerQuestionnaire2.class);
                    startActivity(intent);
            }
        }
    });

OTHER TIPS

If you generate your questions, so you don't have any IDs for your views, you can cycle through all RadioGroups and through all their children in RadioGroup. So if you store RadioGroups for example in an ListArray radioGroups, then you can do this:

private boolean isAllAnswered() {
    for (RadioGroup rg : radioGroups) {
        boolean questionAnswered = false;
        for (int i = 0; i < rg.getChildCount(); i++) { 
            RadioButton rb = rg1.getChildAt(i);
            if (rb.isChecked()) {
                questionAnswered = true;
                break; // litle optimalization
            }
        }
        if (questionAnswered == false) {
            return false;
        }
    }
    return true;
}

If you need to know, which questions are not answered, you can expand it to returning an Array.

If you use xml, then make an common method isAnswered(RadioGroup rg) and skip the first cycle. It's up to you.

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