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);
}
Create shape drawable that represents divider (Called "radio_group_divider"):
Use this drawable as "divider" in the RadioGroup:
Here's a workaround:
First create a Shape Drawable as your divider. Here is an example:
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:
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).
That will work for you. And I am really curious how you add view into Group View? That should cause classcastexception, no ?
If you create RadioGroup programmatically, then you have to set dividers programmatically too. You can do it like that:
...
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"