-3

I tried to write RelativeLayout inside RadioGroup.

but when i written code, it's allow me to choose all Radio button in same time as if the radio button not inside radio group.

i don't want buttons like this:

[] button 1

[] button 2

[] button 3

[] button 4

i want it like this :

[] button 1 [] button 2

[] button 3 [] button 4

<RadioGroup
                            android:id="@+id/radio_gruop_order_type"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:orientation="vertical"
                            android:layout_alignParentTop="true"
                            android:layout_alignParentStart="true">
                            <RelativeLayout
                                android:layout_width="match_parent"
                                android:layout_height="match_parent">


                            <RadioButton
                                android:id="@+id/upgrade"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="@string/upgrade"

                                />

                            <RadioButton
                                android:id="@+id/survey"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="@string/survey"
                                android:layout_alignParentTop="true"
                                android:layout_alignStart="@+id/change_facility" />

                            <RadioButton
                                android:id="@+id/downgrade"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_below="@+id/survey"
                                android:text="@string/downgrade" />

                            <RadioButton
                                android:id="@+id/change_facility"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="@string/change_facility"
                                android:layout_alignBaseline="@+id/downgrade"
                                android:layout_alignBottom="@+id/downgrade"
                                android:layout_alignParentEnd="true"
                                android:layout_marginEnd="17dp" />

                            <RadioButton
                                android:id="@+id/fiber"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_below="@+id/change_facility"
                                android:text="@string/fiber" />

                            </RelativeLayout>


                        </RadioGroup>

this image show what i want to get from the code

  • Can't you use two Radio Groups to achieve that? I don't think you can put a ViewGroup inside a View. – Jonas452 May 11 '17 at 12:10

1 Answers1

2

Use LinearLayout to align your RadioButoon's properly,

Here is the working code:

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:weightSum="2">

                <RadioButton
                    android:id="@+id/upgrade"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Upgrade"/>

                <RadioButton
                    android:id="@+id/survey"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Survey"/>

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:weightSum="2">

                    <RadioButton
                        android:id="@+id/downgrade"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Downgrade"/>

                    <RadioButton
                        android:id="@+id/change_facility"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="change Facility"/>

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:weightSum="2">

                    <RadioButton
                        android:id="@+id/fiber"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Fiber"/>

                </LinearLayout>

        </LinearLayout>
    </RadioGroup>

OUTPUT:

enter image description here

Hope this will help~

Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
  • 1
    Using linearlayout inside radio group makes it loose it's single selection property . Do you have any solution for that ? – Vivek Mishra Jul 02 '21 at 05:39
  • @VivekMishra I have no idea why this is selected as an answer, the point is that whenever you incorporate a Linear or Relative layout inside a RadioGroup, then it looses its functionality of mutually exclusive selections. Workaround would be to just remove radioGroup (since it becomes useless) and use radio buttons only and in their onClick's, you have to preserve its mutually exclusive selections (radio button behavior) if you need I can provide sample code – Araz Apr 05 '22 at 11:10