Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RadioGroup allows multiple RadioButtons to be selected

Tags:

android

I have a RadioGroup defined in XML that has two RadioButtons. However, I need to have the label displayed to the left of the button itself, with the label aligned left and the button aligned right. To do that, I used a RelativeLayout containing a TextView and a RadioButton with no text. This is approximately what the layout looks like:

<RadioGroup android:id="@+id/foo" android:layout_height="wrap_content" android:layout_width="match_parent">
    <RelativeLayout android:layout_height="wrap_content" android:layout_width="match_parent">
        <TextView android:text="@string/bar_one" android:layout_alignParentLeft="true" android:layout_height="wrap_content" android:layout_width="wrap_content" />
        <RadioButton android:id="@+id/bar1" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" />
    </RelativeLayout>
    ...
</RadioGroup>

That displays the way I expect but what I've found is that the RadioGroup allows more than one RadioButton to be selected. When I remove the RelativeLayouts and TextViews, so the RadioButtons are nested directly under the RadioGroup, only one can be selected. Is there a way to wrap my RadioButtons but keep more than one from being selected at the same time?

If not, is there a better way to accomplish the styling I'm after?

like image 898
spaaarky21 Avatar asked Nov 02 '22 20:11

spaaarky21


1 Answers

It would be easier to tell with your full xml since you only show one RadioButton so there's no way to know what the properties of the other one is or a screen shot would be helpful. But you could use a LinearLayout and do something like

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
...>
    <RadioGroup
     ...>
     <TextView
      .../>
     <RadioButton
     .../>
    <TextView
      .../>
    <RadioButton
    .../>
</RadioGroup>
</LinearLayout

But after reading the question again I think you might want something like

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content" 
    android:layout_width="match_parent">

    <RadioGroup 
        android:id="@+id/foo" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"
        android:orientation="vertical"
        android:layout_alignParentRight="true" >

        <RadioButton android:id="@+id/bar1" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" />

        <RadioButton
            android:id="@+id/bar2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RadioGroup>

    <TextView android:id="@+id/textView1"
        android:text="Text 1" 
        android:layout_alignParentLeft="true" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"
        android:layout_alignBottom="@+id/bar1"/>

    <TextView android:text="Text 2" 
        android:layout_alignParentLeft="true" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_alignBottom="@+id/foo" />      
</RelativeLayout>

You may have to adjust some padding and/or margin or make other adjustments to get it just how you want it but I think this will get you close. Let me know if I misunderstood

like image 138
codeMagic Avatar answered Nov 08 '22 09:11

codeMagic