5

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?

spaaarky21
  • 6,524
  • 7
  • 52
  • 65
  • possible duplicate of [Text leftside of a RadioButton with a margin on Android](http://stackoverflow.com/questions/12567950/text-leftside-of-a-radiobutton-with-a-margin-on-android) – MaciejGórski Jun 17 '13 at 22:18
  • @MaciejGórski That question is very close, although it only requires some padding between the button and the label, not for them to be align to opposite sides. I believe aligning them would require a ViewGroup between the RadioGroup and RadioButton (which prevents the selection from being mutually exclusive) but I'm curious what alternatives there might be, if any. – spaaarky21 Jun 17 '13 at 22:24

1 Answers1

2

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

spaaarky21
  • 6,524
  • 7
  • 52
  • 65
codeMagic
  • 44,549
  • 13
  • 77
  • 93