4

I have a ConstraintLayout as a root Layout and it's fine.
However I now have a RadioGroup where I have to make two columns of RadioButtons within it. Since ConstraintLayout is about getting the rid of Nested Layouts, I thought it would be fine placing those RadioButtons in the RadioGroup and place them appropriately.
Turns out having a ConstraintLayout as a Root layout, Containing the RadioGroup, doesn't seem to change anything.
But maybe I'm wrong.

How would you guys achieve having two rows of RadioButtons within a RadioGroup, which is inside a ConstraintLayout?

Cheers

Andy Strife
  • 729
  • 1
  • 8
  • 27

4 Answers4

5

Views have to use layout attributes of their direct parent. You can't, for instance, have RadioButtons with layout_constraints, because the direct parent is a RadioGroup and RadioGroup doesn't know how to interpret those attributes.

RadioGroup extends LinearLayout, so the best you can do with a single RadioGroup is a single row or column of RadioButtons. You could have two RadioGroups in your layout and in your java code listen for changes on both.

private RadioGroup mGroup1; // init in onCreate
private RadioGroup mGroup2; // init in onCreate

private OnCheckedChangedListener mCheckListener = new OnCheckedChangedListener() {

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // To make it appear as if the two groups are one large group,
        // checking something in either should clear the check in the other.
        RadioGroup otherGroup = group == mGroup1 ? mGroup2 : mGroup1;
        otherGroup.clearCheck();

        // do something with checkedId
    }
}
Karakuri
  • 38,365
  • 12
  • 84
  • 104
2

A custom RadioGroup that extends ConstraintLayout is available at Github

You can import the library and use the widget ConstraintRadioGroup

smallfoxy
  • 51
  • 6
1

Please visit https://github.com/samlu/ConstraintRadioGroup A RadioGroup widget that can be used with ConstraintLayout

The blRadioGroup widget should be what you are looking for.

Sam Lu
  • 3,448
  • 1
  • 27
  • 39
  • Code style is awfull... but solution is very nice and neat ... [Here is more android/java codestyle snippet](https://gist.github.com/SelvinPL/5436d4b5a72a5aab055c88c76721073c) – Selvin Mar 02 '22 at 15:35
0

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());
    }
Timothy Winters
  • 5,481
  • 1
  • 15
  • 18