Checking at least one radio button is selected from each radiogroup in android?
Asked Answered
I

5

6

How do I ensure that user is checked at least one radiobutton from each radiogroup. Like I have this radiogroup:

<RadioGroup
    android:id="@+id/myradio_group_one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="first"
        android:checked="true" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="second" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="third" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fourt" />
</RadioGroup>

<RadioGroup
    android:id="@+id/myradio_group_two"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="first"
        android:checked="true" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="second" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="third" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fourt" />
</RadioGroup>

I want to check programmatically if user select at least one radiobutton or not?

Ibnsina answered 18/1, 2014 at 11:37 Comment(0)
F
8
RadioGroup g = (RadioGroup) findViewById(R.id.rBtnDigits); 

// Returns an integer which represents the selected radio button's ID
int selected = g.getCheckedRadioButtonId();

// Gets a reference to our "selected" radio button
RadioButton b = (RadioButton) findViewById(selected);

// Now you can get the text or whatever you want from the "selected" radio button
b.getText();
Fioritura answered 18/1, 2014 at 11:38 Comment(1)
Not from xml, I need this from programmatically.Ibnsina
A
15

Get a reference of the RadioGroup and then call getCheckedRadioButtonId() on the radio group if nothing is selected then the call will return -1.

See public int getCheckedRadioButtonId ()

Anuria answered 18/1, 2014 at 11:45 Comment(0)
F
8
RadioGroup g = (RadioGroup) findViewById(R.id.rBtnDigits); 

// Returns an integer which represents the selected radio button's ID
int selected = g.getCheckedRadioButtonId();

// Gets a reference to our "selected" radio button
RadioButton b = (RadioButton) findViewById(selected);

// Now you can get the text or whatever you want from the "selected" radio button
b.getText();
Fioritura answered 18/1, 2014 at 11:38 Comment(1)
Not from xml, I need this from programmatically.Ibnsina
V
5
if (radioGroup.getCheckedRadioButtonId() != -1)
{
  // hurray at-least on radio button is checked. 
}
else
{
  // pls select at-least one radio button.. since id is -1 means no button is check
}
Velvetvelveteen answered 22/12, 2015 at 9:10 Comment(0)
P
2

This is the code i have used in one of my quiz app.. Have a look at the code if this helps..

private boolean checkAnswer(){
        String answer = getSelection();
        if(answer==null) {
            Log.d("Questions", "No checkbox selected");
            return false;
        }
        else {
        //  Do your stuff here
            return true;
        }

    }


    private String getSelection(){


        if (c1.isChecked())
        {
            return c1.getText().toString();
        }
        if (c2.isChecked())
        {
            return c2.getText().toString();
        }
        if (c3.isChecked())
        {
            return c3.getText().toString();
        }
        if (c4.isChecked())
        {
            return c4.getText().toString();
        }

        return null;

    }
Phlogistic answered 18/1, 2014 at 12:4 Comment(0)
C
1

You can use this method

radioGroup.getCheckedRadioButtonId();

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

As mentioned in the documentation https://developer.android.com/reference/android/widget/RadioGroup.html#getCheckedRadioButtonId()

Cell answered 10/2, 2017 at 17:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.