Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Radio Button in one radio group in 2 horizontal line

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>
like image 466
Shivang Trivedi Avatar asked Jun 19 '15 20:06

Shivang Trivedi


2 Answers

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.

like image 76
Michael Krause Avatar answered Oct 27 '22 19:10

Michael Krause


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>
like image 28
CastRome Avatar answered Oct 27 '22 20:10

CastRome