I came up with a simple class that makes it really easy. It's all in a single java file. It's an "unbound" radio group. You can layout your radio buttons however you like, add the buttons to the group, and it acts just like a radioGroup.
The code is at this gist UnboundRadioGroup, with a full explanation but here's the usage for it:
If you don't want to use an anonymous inner class, you can use implements
public class MainActivity extends AppCompatActivity implements UnboundRadioGroup.OnClickListener
There are a few ways to create a group.
Find the root Viewgroup
ViewGroup viewGroup = (ViewGroup) findViewById(android.R.id.content);
// create a radio group with the root viewgroup
UnboundRadioGroup unboundRadioGroup1 = new UnboundRadioGroup(this, viewGroup);
// don't forget to set a click listener for the group. Using implements in this case
unboundRadioGroup1.setOnClickListener(this);
or create a radio group with the id of the root viewgroup, whichever you prefer.
UnboundRadioGroup unboundRadioGroup2 = new UnboundRadioGroup(this, android.R.id.content);
// add your click listener using an inner class
unboundRadioGroup2.setOnClickListener(new UnboundRadioGroup.OnClickListener()
{
@Override
public void OnClick(RadioButton radioButton)
{
Log.i("radioButton", radioButton.getTag().toString());
}
});
This method manually adds buttons to your group
unboundRadioGroup1.add((RadioButton) findViewById(R.id.radioButton1));
unboundRadioGroup1.add((RadioButton) findViewById(R.id.radioButton2));
and this method automatically adds buttons to your group based on the android:tag property in the XML. Note that you should NOT use this method if you need the tags elsewhere in your code. However, if you are not going to need the tags, you can set the tags of multiple radio buttons to the same name, and then this method will create a group from them
unboundRadioGroup2.createGroupByTag("tag");
If you're using implements instead of inner class, your Onclick would be set like this:
@Override
public void OnClick(RadioButton radioButton)
{
Log.i("radioButton", radioButton.getTag().toString());
}