0

I want a RadioGroup with various RadioButtons displayed in two columns. But it seems that RadioGroup forces the RadioButtons to be displayed in just one column. Is there a way to get the buttons displayed in two columns?

For example, this xml code would put the four buttons in a square form, but inside the RadioGroup they are all in one column.

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

    <RadioButton
        android:id="@+id/radio1"
        android:text="Button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <RadioButton
        android:id="@+id/radio2"
        android:text="Button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="42dp"
        android:layout_marginEnd="42dp"
        android:layout_alignBottom="@+id/radio1"/>

    <RadioButton
        android:id="@+id/radio3"
        android:text="Button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/radio1"
        android:layout_alignLeft="@+id/radio1"
        android:layout_alignStart="@+id/radio1"
        android:layout_marginTop="34dp"/>

    <RadioButton
        android:id="@+id/radio4"
        android:text="Button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/radio3"
        android:layout_alignLeft="@+id/radio2"
        android:layout_alignStart="@+id/radio2"/>
</RadioGroup>
Santiago Gil
  • 1,292
  • 7
  • 21
  • 52
  • 2
    RadioGroup extends LinearLayout, so simply this cannot be done.. You can override RelativeLayout and perform some check/uncheck logic in it. – convexHull Aug 22 '16 at 12:15
  • @convexHull And how could I override it, and get these buttons as a `square form`? – Santiago Gil Aug 22 '16 at 12:20
  • 1
    Check this https://developer.android.com/training/custom-views/index.html Create some class (for example RelativeRadioGroup) and let it extends RelativeLayout. Check out source code for RadioGroup to see how to implement additional logic for buttons checking/unchecking – convexHull Aug 22 '16 at 12:27

1 Answers1

1

Displaying Radio buttons in multiple columns using Radio group cannot be done.However this can be in a different way as below.

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

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

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button1" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="42dp"
            android:layout_marginRight="42dp"
            android:text="Button2" />

    </LinearLayout>

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

        <RadioButton
            android:id="@+id/radio3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button3" />

        <RadioButton
            android:id="@+id/radio4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button4" />

    </LinearLayout>
</LinearLayout>

set View.onClickListener for all radio buttons and handle setting check/uncheck status in your code.

Ramesh R
  • 329
  • 1
  • 6