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 verticalUse of the Barrie
Suppose we have twoTextView
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 TextView
s. 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>