Radio Button in one radio group in 2 horizontal line

7.9k views Asked by At

enter image description here

i try to set 4 radio button in one Radio group in 2 lines, but problem is that when i take linear layout with horizontal orientation then radio group functionality not work . All Radio buttons select . At a time only one button should be select.

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <RadioButton
                    android:id="@+id/r1"

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/lbl1" />

                <RadioButton
                    android:id="@+id/r2"

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/lbl2" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <RadioButton
                    android:id="@+id/r3"

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/lbl3" />

                <RadioButton
                    android:id="@+id/r4"

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/lbl4" />
            </LinearLayout>
        </RadioGroup>
3

There are 3 answers

0
CastRome On BEST ANSWER

i dont know if you still need another option but you can "force" a second line using this:

  1. set the orientation of the RadioGroup horizontal
  2. second set the same marginTop in the radioButtons in the same "line"
  3. third set a negative marginStart on the first element of every (2 and forward) this will mark the star of each line and the other elements will follow

here is an example of this:

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

 <RadioButton
   android:id="@+id/r1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/lbl1" />

   <RadioButton
   android:id="@+id/r2"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/lbl2" />

  <RadioButton
   android:id="@+id/r3"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginStart="-180dp"
   android:layout_marginTop="40dp" 
   android:text="@string/lbl3" />

  <RadioButton
   android:id="@+id/r4"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginStart="0dp"
   android:layout_marginTop="40dp" 
   android:text="@string/lbl4" />

   </RadioGroup>
2
Michael Krause On

RadioGroup does not currently allow nested layouts. (See AOSP issue #8952 for more details)

Because of this, RadioButtons must be direct children of the parent RadioGroup.

That being the case, and noting that RadioGroup extends LinearLayout, I think you're stuck with having to list all of your radio buttons in one row, or in one column.

By the way, there is nothing to stop you from creating your own version of RadioGroup that extends from something more flexible like RelativeLayout. You could start with the code in RadioGroup and adapt it to suit your needs.

0
Tigran Parsadanyan On

To make RadioGroup to have columns, just add GridLayout inside it and change android:columnCount parameter. However you have to override all radio button's OnCheckedChangeListeners, because when you put RadioButtons in GridLayout they are not group anymore.

<RadioGroup

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:columnCount="3">

        <RadioButton
            android:id="@+id/rbtn_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

        <RadioButton
            android:id="@+id/rbtn_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

        .....


        </GridLayout>

    </RadioGroup>