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>
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.
i dont know if you still need another option but you can "force" a second line using this:
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With