I have multiple radio buttons which I want to layout using a table but also include them in a single radio group. I have the following xml layout:
Here is my RadioGroup/RadioButton extension (SoftRadioGroup/SoftRadioButton). RadioGroup is no more needed in the layout XML. You can group the RadioButtons with a property called group.
SoftRadioButton:
import java.util.HashMap;
import java.util.Random;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RadioButton;
public class SoftRadioButton extends RadioButton {
private static HashMap GROUP_MAPPINGS = new HashMap();
private String mGroupName;
public SoftRadioButton(Context context, AttributeSet attrs) {
super(context, attrs);
addToGroup(attrs);
}
public SoftRadioGroup getRadioGroup() {
return GROUP_MAPPINGS.get(mGroupName);
}
private void addToGroup(AttributeSet attrs) {
for (int i = 0; i < attrs.getAttributeCount(); i++) {
if (attrs.getAttributeName(i).equals("group")) {
String groupName = attrs.getAttributeValue(i);
SoftRadioGroup group;
if ((group = GROUP_MAPPINGS.get(groupName)) != null) {
// RadioGroup already exists
group.addView(this);
setOnClickListener(group);
mGroupName = groupName;
} else {
// this is the first RadioButton in the RadioGroup
group = new SoftRadioGroup();
group.addView(this);
mGroupName = groupName;
setOnClickListener(group);
GROUP_MAPPINGS.put(groupName, group);
}
return;
}
}
// group is not specified in the layout xml. Let's generate a random
// RadioGroup
SoftRadioGroup group = new SoftRadioGroup();
group.addView(this);
Random rn = new Random();
String groupName;
do {
groupName = Integer.toString(rn.nextInt());
} while (GROUP_MAPPINGS.containsKey(groupName));
GROUP_MAPPINGS.put(groupName, group);
mGroupName = groupName;
setOnClickListener(group);
}
}
SoftRadioGroup:
import java.util.ArrayList;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
public class SoftRadioGroup implements OnClickListener {
private ArrayList buttons = new ArrayList();
public void addView(RadioButton button) {
buttons.add(button);
}
@Override
public void onClick(View v) {
for (RadioButton button : buttons) {
button.setChecked(false);
}
RadioButton button = (RadioButton) v;
button.setChecked(true);
}
public RadioButton getCheckedRadioButton() {
for (RadioButton button : buttons) {
if (button.isSelected())
return button;
}
return null;
}
public int getChildCount() {
return buttons.size();
}
public RadioButton getChildAt(int i) {
return buttons.get(i);
}
public void check(SoftRadioButton button) {
if (buttons.contains(button)) {
for (RadioButton b : buttons) {
b.setChecked(false);
}
}
}
}
Usage in XML layout embedded in a table (2 groups with 2 buttons per group):