2

I have made a radiogroup and inside it I have made two linear layouts and in each linear layout I have added two radio buttons.

But the problem is when I run the app multiple radio buttons get selected i.e. they aren't acting mutually exclusive.

Following is my XML file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_simple_maths_game"
    android:layout_width="match_parent"    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.root.mysimplemathsgame.SimpleMathsGame"  android:background="#000"
    android:orientation="vertical"    android:baselineAligned="false">
<LinearLayout
    android:layout_weight="0.4"                android:orientation="horizontal"
    android:layout_width="match_parent"        android:layout_height="0dp">

<TextView
    android:text="@string/mytext"                    android:id="@+id/tvText"
    android:layout_width="wrap_content"                     android:layout_height="0dp"
    android:layout_weight="0.87"                            android:textColor="#fff"
    android:textAppearance="@style/TextAppearance.AppCompat.Large"        android:padding="20dp"
    android:gravity="center_vertical"                   android:layout_gravity="center_vertical"
    android:textSize="32sp" />
<LinearLayout
    android:layout_weight="1"                  android:background="#fff"
    android:layout_width="match_parent"        android:layout_height="0dp"
    android:orientation="horizontal">
    <RadioGroup
        android:paddingLeft="30dp"                 android:id="@+id/rgOptions"
        android:layout_width="match_parent"        android:layout_height="match_parent">
        <LinearLayout
            android:layout_weight="1"                 android:orientation="horizontal"
            android:layout_width="match_parent"                android:layout_height="0dp">
            <RadioButton
                android:layout_weight="1"             android:textColor="#000"
                android:id="@+id/rb1"                 android:text="@string/answer1"
                android:layout_width="0dp"            android:layout_height="match_parent" />
            <RadioButton
                android:layout_weight="1"             android:textColor="#000"
                android:id="@+id/rb2"                 android:text="@string/answer2"
                android:layout_width="0dp"            android:layout_height="match_parent" />
        </LinearLayout>
        <LinearLayout
            android:layout_weight="1"                 android:orientation="horizontal"
            android:layout_width="match_parent"       android:layout_height="0dp">
            <RadioButton
                android:textColor="#000"
                android:layout_weight="1"             android:layout_gravity="start"
                android:id="@+id/rb3"                 android:text="@string/answer3"
                android:layout_width="0dp"            android:layout_height="match_parent" />
            <RadioButton
                android:textColor="#000"
                android:layout_weight="1"             android:layout_gravity="end"
                android:id="@+id/rb4"                 android:text="@string/answer4"
                android:layout_width="0dp"            android:layout_height="match_parent" />
        </LinearLayout>
    </RadioGroup>
</LinearLayout>
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"        android:layout_height="wrap_content">
    <Button
        android:layout_weight="1"
        android:text="@string/end"                android:id="@+id/bEnd"  android:layout_gravity="center_horizontal"
        android:layout_width="match_content"            android:layout_height="wrap_content" />
</LinearLayout>

ADM
  • 20,406
  • 11
  • 52
  • 83
JDFuzyll
  • 77
  • 9
  • i think it is because your button is wrapped by linear layout. try to place radiobutton directly inside radiogroup and see if it works – firmanslash Oct 29 '16 at 04:01
  • 2
    Possible duplicate of [How to group RadioButton from different LinearLayouts?](http://stackoverflow.com/questions/10461005/how-to-group-radiobutton-from-different-linearlayouts) – jamesjaya Oct 29 '16 at 04:02
  • radiogroup can only contain radiobuttons if you put layout in it and radio buttons in that layout it's not gonna work for sure just remove the layout and make separate radiogroup and put your radio buttons inside that radiogroup – hatib abrar Oct 29 '16 at 04:42

2 Answers2

4

RadioGroup is a subclass of Linearlayout. RadioGroup can contain only RadioButton as immediate child for its to work as a group . If you put a layout inside it then will work just as a layout and multiple Radiobuttons can be selected . So the answer to the question remove the LinearLayout . If you want orientation RadioGroup has Orientation attribute inside it you can directly add Orientation inside RadioGroup.

ADM
  • 20,406
  • 11
  • 52
  • 83
0

Try This One....

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_simple_maths_game"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="vertical"
android:weightSum="2">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/tvText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="center_vertical"
        android:padding="20dp"
        android:text="My Text"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#000"
        android:textSize="32sp" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal">

    <RadioGroup
        android:id="@+id/rgOptions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="30dp">

        <RadioButton
            android:id="@+id/rb1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="answer1"
            android:textColor="#000" />

        <RadioButton
            android:id="@+id/rb2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="answer1"
            android:textColor="#000" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/rb3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="answer1"
            android:textColor="#000" />


        <RadioButton
            android:id="@+id/rb4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="answer1"
            android:textColor="#000" />

    </LinearLayout>

    </RadioGroup>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5"
    android:orientation="horizontal">

    <Button
        android:id="@+id/bEnd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1"
        android:text="end" />
</LinearLayout>