Android radiogroup, divider between radiobuttons

Karakuri picture Karakuri · May 22, 2012 · Viewed 21.3k times · Source

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);
}

Answer

smilingky picture smilingky · Feb 4, 2014
<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 ?