I want to give the user four choices, the first and second choices on a row and the third and fourth choices on another row. My problem is when the application starts I can select more than one choice, but I don't want that. This is my xml layout:
<RadioGroup
android:id="@+id/rgAnswerQuestionChoices"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rAnswerQuestonChoic1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton"
android:visibility="invisible" />
<RadioButton
android:id="@+id/rAnswerQuestionChoice2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton"
android:visibility="invisible" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rAnswerQuestionChoice3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton"
android:visibility="invisible" />
<RadioButton
android:id="@+id/rAnswerQuestionChoice4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton"
android:visibility="invisible" />
</LinearLayout>
</RadioGroup>
What am I doing wrong?
If you place other layouts between the
RadioButtons
and the parentRadiogroup
(like you did with thoseLinearLayouts
) then the mutual exclusion will not work anymore.To put those
RadioButtons
in a two rows table you could make your ownRadioGroup
which places theRadioButtons
like you want or you can try to simulate that layout by having twoRadioGroups
that behave as one(for example, RadioGroup with two columns which have ten RadioButtons).I know its late but I post it for anybody that face this problem in android. if you use
RadioGroup
in android, you can't putRadioButtons
anywhere you want except the exact child of yourRadioGroup
with no depth of hierarchy. so if you want to put yourRadioButtons
in aLinearLayout
or manage it in a grid, buttons don't act like their belongs to the group (they will stay checked if the user check anotherRadioButton
in the group).My solution
I wrote a class named
RadioGroupCheckListener.java
and simply use this line of code to declare a group:you can find
RadioGroupCheckListener.java
in my github RadioGroupCheckListener