18

So I have the following radiobuttons. I want to have them display like this:

enter image description here

However this occurs:

radiogroup problem

How I can get it to display like above?
I can move in the GUI editor in Eclipse it but it removes the RadioButton from the RadioGroup!
Within the group, it ignores all other layout parameters.

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/timeBar"
    android:layout_marginTop="43dp"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/privRadio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Everyone" />

    <RadioButton
    android:id="@+id/privRadio1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:text="FriendOfFriends" />


 <RadioButton
    android:id="@+id/privRadio2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Friends" />
</RadioGroup>
TheRealKingK
  • 829
  • 7
  • 14

2 Answers2

12

You can simply copy this class:

https://github.com/jevonbeck/AbstractMachine/blob/jevon_dev/app/src/main/java/org/ricts/abstractmachine/ui/utils/MultiLineRadioGroup.java

into an appropriate package in your project and instantiate in XML like so:

<view
    class="mypackage.packagepath.MultiLineRadioGroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"/>
J. Beck
  • 746
  • 6
  • 11
  • Looks like great code, but doesn't really accommodate for margins for the RadioButtons in the RadioGroup. – welshk91 May 24 '17 at 22:02
  • This works fine except when setting drawable selectors on the radio buttons, then it doesnt run. Any solution to this? – Darth Plagueris Dec 13 '18 at 00:49
  • This has some issues when used programmatically. For instance, `OnCheckedChangeListener` gives incorrect index when radio button changed at runtime. Another issue is, `margin` is lost when inflating buttons through code. – Mangesh Jun 23 '20 at 18:07
1

What you are asking for is a FlowLayout. Such a layout has the benefit of only wrapping when it's needed, as opposed to 0gravity's more "static" solution.

Joel Sjögren
  • 2,010
  • 1
  • 13
  • 12
  • Interesting, is it possible to use this inside a RadioGroup? – TheRealKingK Aug 09 '12 at 20:42
  • I believe it's not, but it is possible to use RadioButtons without assigning them to a group. – Joel Sjögren Aug 10 '12 at 14:34
  • 2
    @TheRealKingK: If you sub-class a RadioGroup and implement the onMeasure() and onLayout() functions similarly to the FlowLayout, you will get the solution you are looking for. See my solution on this page. – J. Beck Jun 03 '16 at 12:58