6

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>
Shivang Trivedi
  • 2,182
  • 1
  • 20
  • 26

3 Answers3

4

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.

Michael Krause
  • 4,689
  • 1
  • 21
  • 25
  • then how can handle functionality like select one option from given four options? – Shivang Trivedi Jun 19 '15 at 21:24
  • You can programmatically set up this mutually exclusive scoping by using setOnCheckedChangeListeners for each radio button in your group, and when you see a check change, uncheck all the others in your logical group. – Michael Krause Jun 19 '15 at 21:27
0

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>
CastRome
  • 36
  • 8
-1

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>