toggle visibility of chain group in constraint layout toggle visibility of chain group in constraint layout xml xml

toggle visibility of chain group in constraint layout


Yes, so now in ConstraintLayout also we can handle visibility of particular groups of Views using the Group

This is a new feature introduced in ConstraintLayout which is currently in Beta version.

How to add beta ConstraintLayout to your project? Follow the steps below:

Add maven support in project gradle file as below

allprojects {    repositories {        maven { url 'https://maven.google.com' }        jcenter()    }}

Then in app gradle dependencies add ConstraintLayout library dependency

compile 'com.android.support.constraint:constraint-layout:1.1.0-beta3'

Now you have to add a Group in your ConstraintLayout as follows

<android.support.constraint.Group        android:layout_width="wrap_content"        android:layout_height="wrap_content"        app:constraint_referenced_ids="button7,button3,button2"        android:id="@+id/group" />  

Where in Group, the reference id...

app:constraint_referenced_ids="button7,button3,button2"

... contains the comma separated view id's you want to handle run time, so, in an Activity, you just bind the Group as below and handle the visibility

import android.support.constraint.Group; //import statement in activityGroup group=(Group)findViewById(R.id.group); //bind view from xmlgroup.setVisibility(View.VISIBLE); //this will visible all viewsgroup.setVisibility(View.GONE); //this will set Gone to all viewsgroup.setVisibility(View.INVISIBLE); //this will set INVISIBLE to all view

EDIT: ConstraintLayout 1.1.0 stable version was released on 12 April 2018 https://androidstudio.googleblog.com/2018/04/constraintlayout-110.html

implementation 'com.android.support.constraint:constraint-layout:1.1.0'

Edit for Android X: If anyone is using the Android X package, you can find package info here

https://developer.android.com/jetpack/androidx/migrate


If you are using beta version of constraintlayout, please follow @pavan's answer

If you are using AndroidX, Please follow below steps to integrate constraintlayout and Group:

1) Add AndroidX constraint layout dependancy in your project:

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

2) Use ConstraintLayout Group as below in your project:

<androidx.constraintlayout.widget.Group                    android:id="@+id/groupDetails"                    android:layout_width="wrap_content"                    android:visibility="gone" // Default visibility for group views                    app:constraint_referenced_ids="textViewUserName, ..." // id's which you want to include in group                    android:layout_height="wrap_content"/>

3) Here is the coding part for toggle visibility:

private lateinit var groupDetails:Group...groupDetails = findViewById(R.id.groupDetails)groupDetails.visibility = View.GONE // Change visibility

Hope it's helps while using AndroidX.


You can use that same Linear Layout as child of Constraint Layout too, OR you can also make all those 3 widgets as child of another Constraint Layout and make that layout as child of main Constraint Layout.

Then you can maintain the visibility and other stuff same as before.