24

I'm experimenting with "Constraint.Group" and I've children views: A, B, C.

In code, "Constraint.Group".visibility = View.Gone does work, but if I choose to do A.visibility = View.Gone it does not take an effect on children view. Is this normal behaviour?

MaaAn13
  • 264
  • 5
  • 24
  • 54

4 Answers4

23

Update: The behavior of individual view visibility within a group has been change and is reported as fixed in ConstraintLayout version 2.0.0 beta 6. See bug fixes for ConstraintLayout 2.0.0 beta 6 .


It does look like group visibility trumps the visibility of individual views of the group. This makes sense since each view has some visibility defined (GONE, VISIBLE, INVISIBLE) so, if an individual view's visibility setting was honored, the integrity of the group would be violated. In other words, the individual view we changed the visibility of would, in essence, not be part of the group.

Cheticamp
  • 61,413
  • 10
  • 78
  • 131
5

I agree with Cheticamp and would like to add that you've got to toggle visibility individually as he said, or either create a general group to change all views inside and a local group to change only a specific view, like below:

<ImageView
    android:id="@+id/view1"
    android:layout_width="0dp"
    android:layout_height="0dp"
    />

<ImageView
    android:id="@+id/view2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    />

<android.support.constraint.Group
        android:id="@+id/group1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible"
        app:constraint_referenced_ids="view1,view2" />

<android.support.constraint.Group
        android:id="@+id/group2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:constraint_referenced_ids="view1" />

It won't be possible to change a single view visibility that is inside a group, but this way you can change group1 visibility or group2 visibility.

1

If you use above 2 version of constraint layout, you should use isGone property. group.isGone = true But if you use version lower than 2, it doesn't work due to some bugs.

Umut ADALI
  • 1,146
  • 1
  • 14
  • 31
0

Take a look inside the source code of ConstraintLayout:

enter image description here

Looks like ConstraintLayout will take out visibility and elevation of its child view, and rebind those value back to the child view itself.

So setting visibility to child view inside Group is not working.

Some workaround would be referencing single view to another Group (it might look tricky but still acceptable to me).

Anthonyeef
  • 2,595
  • 1
  • 27
  • 25