Can't set visibility on constraint group

19,958

Solution 1

Update: This was reported as fixed in ConstraintLayout version 2.0.0 beta 6. See bug fixes for ConstraintLayout 2.0.0 beta 6 .



This looks like a bug to me. GONE works but INVISIBLE doesn't and I think it should. It may be worth a bug report unless someone can post where my thinking is wrong. (I am using constraint-layout:1.1.0-beta4.)

In the meantime, here is a work-around that explicitly retrieves the ids within the group and sets the visibility of each retrieved view.

Within MainActivity.kt

private fun toggleLoginUI(show: Boolean) {
    if (show) {
        setGroupVisibility(mLayout, group, Group.VISIBLE)
    } else {
        setGroupVisibility(mLayout, group, Group.INVISIBLE)
    }
}

private fun setGroupVisibility(layout: ConstraintLayout, group: Group, visibility: Int) {
    val refIds = group.referencedIds
    for (id in refIds) {
        layout.findViewById<View>(id).visibility = visibility
    }
}

mLayout is the ConstraintLayout.

Update: Here is another work-around that takes advantage of the fact that changing to/from GONE works as expected:

private fun toggleLoginUI(show: Boolean) {
    if (show) {
        group.visibility = GONE
        group.visibility = VISIBLE
    } else {
        group.visibility = GONE
        group.visibility = INVISIBLE
    }
}

Solution 2

You can also simply call requestLayout method after changing Group visibility to View.INVISIBLE.

fun makeGroupInvisible(group: Group) {
    group.visibility = View.INVISIBLE
    group.requestLayout()
}

Problem is that android.support.constraint.Group updates visibility of its members in updatePreLayout method which is called from onMeasure in ConstraintLayout.

Solution 3

android.support.constraint.Group has a public method

 public void updatePreLayout(ConstraintLayout container) {
   ...
 }

that updates children visibilities, so calling

dataGroup.visibility = if (visible) View.VISIBLE else View.INVISIBLE
dataGroup.updatePreLayout(root)

worked for me

Solution 4

Had the same problem and nothing from above helps. My solution was to setVisibility(viewId, ConstraintSet.VISIBLE) inside constraint set and apply that to ConstraintLayout view.

For example:

myContstraintSet.apply {
    setVisibility(firstGroup.id, ConstraintSet.VISIBLE)
    setVisibility(secondGroup.id, ConstraintSet.GONE)

    connect(oneView.id, ConstraintSet.BOTTOM, R.id.secondView, ConstraintSet.TOP)
    clear(anotherView.id, ConstraintSet.TOP)
}
myContstraintSet.applyTo(myConstraintLayout)
Share:
19,958

Related videos on Youtube

SS2095
Author by

SS2095

Updated on June 07, 2020

Comments

  • SS2095
    SS2095 almost 4 years

    When i try to set the visibility of the group on button click,it doesn't affect the view's visibility.Using com.android.support.constraint:constraint-layout:1.1.0-beta4. I've tried setting it element-wise without problems,but no success with the group.

    My MainActivity.kt

    private fun toggleLoginUI(show: Boolean) {
        if (show) {
            group.visibility = VISIBLE
        } else {
            group.visibility = INVISIBLE
        }
    }
    
    fun onClick(view: View) {
        when (view.id) {
            R.id.button -> toggleLoginUI(true)
            R.id.button4 -> toggleLoginUI(false)
        }
    }
    

    My activity_main.xml

        <android.support.constraint.ConstraintLayout..
    
                <TextView
                    android:id="@+id/textView"
    ... />
    
                <TextView
                    android:id="@+id/textView2"
    ... />
    
                <Button
                    android:id="@+id/button"
    .../>
    
                <Button
                    android:id="@+id/button4"
    ... />
    
    
                <android.support.constraint.Group
                    android:id="@+id/group"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:visibility="visible"
                    app:constraint_referenced_ids="textView,textView2" />
                </android.support.constraint.ConstraintLayout>
    
    • Hemant Parmar
      Hemant Parmar over 6 years
      have look this
    • Divyesh Kalotra
      Divyesh Kalotra over 6 years
      whats the problem give the log
    • SS2095
      SS2095 over 6 years
      there is no error in the log.it just doesn't do anything
    • Rohit Sharma
      Rohit Sharma over 4 years
      Was beating my head with screen to figure this out.
  • SS2095
    SS2095 over 6 years
    that works but was hoping for a way that didn't require having to loop through the elements.thanks for the answer.hope it gets resolved.
  • Cheticamp
    Cheticamp over 6 years
    @SS2095 If it's a bug then you will have to wait for a future release. (And I think that it is a bug.) This is the best that can be done in the interim if you need that functionality.
  • filipproch
    filipproch almost 6 years
    I don't agree with this answer, this question is specifically about using View.INVISIBLE. Using ConstraintLayout.GONE is the same as using View.GONE. It's a different type of visibility setting that works.
  • Richard Le Mesurier
    Richard Le Mesurier over 5 years
    This makes no sense that this should be the fix, but it is. Clearly something about group is buggy and likely related to Android Studio.
  • jhfdr3s
    jhfdr3s over 5 years
    This doesn't help me
  • Sushant Garg
    Sushant Garg over 5 years
    Please share your code so that we can understand your problem
  • Torsten Grote
    Torsten Grote about 5 years
    Has somebody reported that bug and has a link to it?
  • Cheticamp
    Cheticamp about 5 years
    @TorstenGrote Maybe issuetracker.google.com/issues/117485026? I also see a reference to a ConstraintLayout 2.0 "Layer" feature that may be somehow applicable.
  • Mayeul sgc
    Mayeul sgc about 5 years
    Thx, spent 2 days to find that 'requestLayout()' solver <3
  • Nicolás Vera
    Nicolás Vera almost 5 years
    This answer is simple, works and uses a method provided by the library. This should be the correct answer
  • Lee Boon Kong
    Lee Boon Kong over 4 years
    Thanks! This worked for me, but can this be considered as a bug of constraint layout? Calling requestLayout() after every visibility change seems not quite normal
  • DannyP
    DannyP over 4 years
    group.requestLayout() fixed it for me as well. That must be a bug
  • Julien Arzul
    Julien Arzul over 4 years
    I actually raised an issue related to this a while ago as I agree with @Cheticamp that this is a bug in the Group class: issuetracker.google.com/issues/130524019