What is Barrier in ConstraintLayout ?

Barries is view which makes a line either horizontal or vertical. Like guideline barrier is only visible at design time and invisible to end user at runtime.

View make a reference constrain to the barrier. Please note that the position of the barrier is not fixed but it is flexible. Barrier adjusts its position with refrence to the largest view in width and talest view in. Moreover if a view has no reference constrain with the barrier it is pushed to the side to while barrier adjust its position.

How to create the Barrier

Creating barrier is quite simple in android studio. Just drag and drop the barrier icon on the android studio layout editor. Then you can make the constrain from any view which you want accordingly to this barrie. Barrier  can be put as either horizontal or vertical

Use of the Barrie   

Suppose we have two TextView widgets with dynamic heights and you want to place a Button just below the tallest TextView:

The way to implement that directly in the layout is to use a horizontal Barrier. That Barrier allows you to specify a constraint based on the height of those two TextViews. Then you constrain the top of your Button to the bottom of the horizontal Barrier

 







<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/left_text_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        android:textSize="16sp"
        android:background="#AAA"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/right_text_view"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/right_text_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:text="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
        android:textSize="16sp"
        android:background="#DDD"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/left_text_view"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.constraint.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="left_text_view,right_text_view" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/barrier" />

</android.support.constraint.ConstraintLayout>

Difference between barrier and guideline

The main difference between the barrier and guide is that barrier position is adjustable and always based on the size of the multiple views contained within it while guideline position is always fixed.

Group

A group is a grouping of some views logically. But is different from viewGroup keep in mind that viewGroup are the containers for other views in android. But group means not the containers or nesting the views. For example we can make the group of certain views if we want their visibility gone collectively to achieve some functionality in our Android App.