Radio Button in one radio group in 2 horizontal line
Asked Answered
B

3

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>
Blanchette answered 19/6, 2015 at 20:54 Comment(2)
Can you add the rest of your layout, please?Mice
i put one image over here here , i can set layout this way but radio group functionality not work after thatBlanchette
O
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>
Overlarge answered 9/2, 2017 at 20:33 Comment(0)
M
5

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.

Mice answered 19/6, 2015 at 21:6 Comment(2)
then how can handle functionality like select one option from given four options?Blanchette
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.Mice
O
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>
Overlarge answered 9/2, 2017 at 20:33 Comment(0)
S
-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>
Smarm answered 6/4, 2020 at 14:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.