Aksh Quant
Aksh Quant

Reputation: 11

RadioButton already selected

I'm using a RadioGroup in my layout.
When I run the program, I'm getting the first RadioButton already selected out of 4 RadioButtons.
I want none of these RadioButtons initially selected.

Upvotes: 0

Views: 108

Answers (1)

AskNilesh
AskNilesh

Reputation: 69689

If you want to set non of your RadioButton to selected by default than use android:checked="false" in your RadioButton

SAMPLE CODE

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/choiceOne"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:checked="false"
            android:text="choiceOne" />

        <RadioButton
            android:id="@+id/choiceTwo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:checked="false"
            android:text="choiceTwo" />

        <RadioButton
            android:id="@+id/choiceThree"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:checked="false"
            android:text="choiceThree" />

        <RadioButton
            android:id="@+id/choiceFour"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:checked="false"
            android:text="choiceFour" />

    </RadioGroup>

EDIT

if you want to set default radio button to selected than there is two way

  1. You can use android:checked="true" in particular RadioButton

  2. You can use android:checkedButton="@+id/choiceOne" in your RadioGroup

    The id of the child radio button that should be checked by default within this radio group.

SAMPLE CODE FOR OPTION TWO

    <RadioGroup
        android:layout_width="match_parent"
        android:checkedButton="@+id/choiceOne"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/choiceOne"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="choiceOne" />

        <RadioButton
            android:id="@+id/choiceTwo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="choiceTwo" />

        <RadioButton
            android:id="@+id/choiceThree"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="choiceThree" />

        <RadioButton
            android:id="@+id/choiceFour"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="choiceFour" />

    </RadioGroup>

Upvotes: 4

Related Questions