One bit of task that I find myself doing over and over again is managing a bunch of Views and their visibility. In the olden days <insert old person handwave>, before there was ConstraintLayout, I have written my fair share of container_s to make this task manageable. Say we have to do something like this:

Now you see me, now you don't!

If you asked me two years ago to implement the layout above, I would have:

  • wrapped the divider, the section title, and that extra text in a LinearLayout
  • added an OnCheckedChangeListener to the CheckBox
  • managed the LinearLayout’s visibility in the listener implementation

Or in code:

@BindView( CheckBox mCheckbox;                                                   
@BindView( ViewGroup mContainer;                                                
public void onCreate(@Nullable Bundle savedInstanceState) {                                    
    mCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {        
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
            if (isChecked) {                                                                   
            } else {                                                                           

(Here is the Activity code in full, as well as the layout file).

It works, but it’s a whole lot of things to write!

Let’s try to harness the power of the tools we currently have at our disposal. First off, here’s the skeletal ConstraintLayout-ified version of our existing layout file (click here for the full version):

<layout xmlns:android="">

        <ImageView android:id="@+id/image_1" />

        <ImageView android:id="@+id/image_2" />

        <TextView android:id="@+id/description" />

        <CheckBox android:id="@+id/checkbox" />

        <View android:id="@+id/divider" />

        <TextView android:id="@+id/section_description" />

        <TextView android:id="@+id/section_title" />

        <Button android:id="@+id/button_2"/>

Immediately we see that the view hierarchy is so much flatter, and there are no unnecessary nestings at all! But then, the next question arises: doesn’t this mean that when we have to deal with managing the visibility of the required widgets we could either end up writing more code or introduce some level of nesting?

Well, not quite! We can use a ConstraintLayout feature called Group that would make managing our desired behaviour much much easier. A Group allows us to reference a comma-separated list of widget IDs and directly manage their visibility – which is exactly what we want to achieve!

Let’s go ahead and make the Group:


And now all that’s left is actually hiding or showing it. For this demo, I opted to use the same technique as in my previous post and use a data binding expression:

android:visibility="@{checkbox.checked ? View.VISIBLE : View.GONE}"

We can now simplify our Activity code to become:

public override fun onCreate(savedInstanceState: Bundle?) {
    DataBindingUtil.setContentView<ActivityConstraintBinding>(this, R.layout.activity_constraint)