27

Is there a simple way to add a divider between RadioButtons inside a RadioGroup? I've tried using the divider xml attribute and it doesn't seem to be working. In case it's relevant, the RadioGroup in my layout does not contain any child views; I'm adding the RadioButtons programmatically.

EDIT: Problem solved. You can add views besides RadioButton inside RadioGroup in the xml. In my case, you can also do it programmatically, but be careful about your layout params. Akki had the right idea, and this worked for me:

for (int i = 0; i < items.size(); i++) {
    if (i > 0) {
        // add a divider with height of 1 pixel
        View v = new View(this);
        v.setLayoutParams(new RadioGroup.LayoutParams(LayoutParams.MATCH_PARENT, 1));
        v.setBackgroundColor(android.R.color.darker_gray);
        mRadioGroup.addView(v);
    }
    RadioButton rb = new RadioButton(this);
    /* set other properties ... */

    mRadioGroup.addView(rb);
}
Zachary Schuessler
  • 3,644
  • 2
  • 28
  • 43
Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • It may be helpful if you could provide an image of what you're trying to make. – mtmurdock May 22 '12 at 16:46
  • 4
    radio group is derived from linear layout so create a view object and add it after each radio button except last one. – Akram May 22 '12 at 17:08
  • mtmurdock: I just need a thin horizontal line between each item. Akki: I thought of that, but was hoping there was something simpler. It seems like such a fairly obvious use case that one would expect Google to have made provisions for it within the framework. – Karakuri May 22 '12 at 17:34

4 Answers4

37
<RadioGroup
    android:id="@+id/location_radio_group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="?android:attr/dividerHorizontal"
    android:showDividers="middle">
</RadioGroup>

That will work for you. And I am really curious how you add view into Group View? That should cause classcastexception, no ?

Anirudh Sharma
  • 7,968
  • 13
  • 40
  • 42
smilingky
  • 379
  • 3
  • 4
  • This does not work for me. It has no effect. But if I define and assign my own shape for android:divider for the RadioGroup (not the individual buttons) in an xml file, it works. Anyone know why? – Alyoshak Sep 04 '18 at 19:09
  • (Btw, this didn't work for me, until I added an orientation vertical to my RadioGroup) – Bertrand Jul 16 '19 at 15:37
16

Here's a workaround:

First create a Shape Drawable as your divider. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
<solid 
    android:color="@color/white" />
<stroke 
    android:width="0.3dp" 
    android:color="@color/black" />
</shape>

This is just a simple black border. Put it inside your drawable/ folder and name it something like custom_divider.xml.

Then, go to your layout which uses a RadioGroup. Use the ShapeDrawable as a background for each of the RadioButton(s). Here is an example:

<RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:divider="@color/black" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/custom_radiogroup_divider"
            android:checked="true"
            android:text="RadioButton" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/custom_radiogroup_divider"
            android:text="RadioButton" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/custom_radiogroup_divider"
            android:text="RadioButton" />

    </RadioGroup>

You can also add a ShapeDrawable to your RadioGroup. It depends on you, customize it if you need. :)

Here is my example of a RadioGroup with custom border (with corner radius) and custom divider ShapeDrawable(s). RadioGroup divider

KarenAnne
  • 2,764
  • 1
  • 28
  • 21
7

Create shape drawable that represents divider (Called "radio_group_divider"):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <size android:width="12dp" />
    <solid android:color="@color/transparent" />
</shape>

Use this drawable as "divider" in the RadioGroup:

<RadioGroup
        ...
        android:divider="@drawable/radio_group_divider"
        android:showDividers="middle"
        ...>
</RadioGroup>
David
  • 37,109
  • 32
  • 120
  • 141
0

If you create RadioGroup programmatically, then you have to set dividers programmatically too. You can do it like that:

RadioGroup radioGroup = new RadioGroup(getActivity()); //We create a new radio group
radioGroup.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
radioGroup.setDividerDrawable(getResources().getDrawable(android.R.drawable.divider_horizontal_textfield, getActivity().getTheme()));

...

Note: This code part is working in a fragment. So if you want to put it in to an activity, you have to modify by changing "getActivity()" with "YourActivityName.this"