Nahidaa Nahidaa - 3 months ago 10
Android Question

Valid layout params for RadioButtons

What layout parameter could I use to make my RadioButtons

layout_alignParentStart
and Below a View? In Android studio, doing this creates an error. Or is there an attribute of some sort I could add to the RadioGroup?

here is the code:

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

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_button"
android:id="@+id/radioButton"
android:layout_below="@+id/text7"
android:layout_alignParentStart="true"
android:textSize="12sp"
android:textColor="#000000"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_button2"
android:id="@+id/radioButton2"
android:layout_below="@+id/radioButton"
android:layout_alignParentStart="true"
android:textSize="12sp"
android:textColor="#000000"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_button3"
android:id="@+id/radioButton3"
android:layout_below="@+id/radioButton2"
android:layout_alignParentStart="true"
android:textSize="12sp"
android:textColor="#000000"
android:onClick="onRadioButtonClicked"/>

Answer

Here's the deal: RadioGroup extends LinearLayout, and that means that RadioGroup wants all the radio buttons to be in a line, either horizontal or vertical.

If you need radio buttons in a grid or other non-linear layout, you can use something like RelativeLayout as a parent, but then you will have to listen to each one of the radio buttons directly and code the logic that turns the previous one off when the next one is selected.