How to check if radio buttons are selected on button click?

Steve

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

                }


    });
}
Sami Eltamawy

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);
            }
        }
    });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to check a radio button with jQuery?

From Dev

AngularJS radio buttons not marked $dirty until last button selected

From Dev

How to check if the radio button is selected or not in Selenium WebDriver?

From Dev

jQuery Check if a Radio Button is Selected or not

From Dev

Shiny - How to Style a Selected Radio Buttons Label?

From Dev

Check for selected radio button

From Dev

Dynamically creating radio buttons and selected appropriate button Angularjs

From Dev

How to keep radio button selected after refresh the jsp page click in radio button

From Dev

How to check which radio button is selected if they're all runat server?

From Dev

On click check the closest radio button within div

From Dev

How to check radio button with radio button value

From Dev

Jquery for pass selected radio button value on button click

From Dev

JavaScript validation to check if a radio button is selected?

From Dev

How to check if the radio button is selected or not in Selenium WebDriver?

From Dev

How to check if any of the Radio buttons are checked or not when clicked on submit button

From Dev

Getting the combination of selected radio buttons to show message on button click

From Dev

how to make radio buttons selected in angularjs

From Dev

check all radio list radio button has been selected or not on gridview

From Dev

How to check which radio button is selected if they're all runat server?

From Dev

How do i check if a specific Radio button is checked among the other radio buttons using Javascript?

From Dev

show/hide buttons on radio button click jquery

From Dev

jQuery selected radio button index among different sets of radio buttons

From Dev

How to style selected radio button

From Dev

How to disable buttons based on which radio button is selected?

From Dev

How do I check which radio button is selected when there are multiple radio buttons with the same name? (using jquery)

From Dev

Is there a way to check if the nth radio button is selected in jQuery?

From Dev

Click on Radio button after boolean check

From Dev

Check if Radio Button is clicked (Selected) - Smarty Template

From Dev

Check if a radio button is selected

Related Related

  1. 1

    How to check a radio button with jQuery?

  2. 2

    AngularJS radio buttons not marked $dirty until last button selected

  3. 3

    How to check if the radio button is selected or not in Selenium WebDriver?

  4. 4

    jQuery Check if a Radio Button is Selected or not

  5. 5

    Shiny - How to Style a Selected Radio Buttons Label?

  6. 6

    Check for selected radio button

  7. 7

    Dynamically creating radio buttons and selected appropriate button Angularjs

  8. 8

    How to keep radio button selected after refresh the jsp page click in radio button

  9. 9

    How to check which radio button is selected if they're all runat server?

  10. 10

    On click check the closest radio button within div

  11. 11

    How to check radio button with radio button value

  12. 12

    Jquery for pass selected radio button value on button click

  13. 13

    JavaScript validation to check if a radio button is selected?

  14. 14

    How to check if the radio button is selected or not in Selenium WebDriver?

  15. 15

    How to check if any of the Radio buttons are checked or not when clicked on submit button

  16. 16

    Getting the combination of selected radio buttons to show message on button click

  17. 17

    how to make radio buttons selected in angularjs

  18. 18

    check all radio list radio button has been selected or not on gridview

  19. 19

    How to check which radio button is selected if they're all runat server?

  20. 20

    How do i check if a specific Radio button is checked among the other radio buttons using Javascript?

  21. 21

    show/hide buttons on radio button click jquery

  22. 22

    jQuery selected radio button index among different sets of radio buttons

  23. 23

    How to style selected radio button

  24. 24

    How to disable buttons based on which radio button is selected?

  25. 25

    How do I check which radio button is selected when there are multiple radio buttons with the same name? (using jquery)

  26. 26

    Is there a way to check if the nth radio button is selected in jQuery?

  27. 27

    Click on Radio button after boolean check

  28. 28

    Check if Radio Button is clicked (Selected) - Smarty Template

  29. 29

    Check if a radio button is selected

HotTag

Archive