Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 996
androidcodehunter Avatar asked Jan 18 '14 11:01

androidcodehunter


1 Answers

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 ()

like image 82
iulius Avatar answered Sep 21 '22 02:09

iulius