Recently trying to implement Constraint Layout
but I found Barrier
and Guideline
works same. Both works like divider. Is there any difference between them?

- 7,526
- 14
- 42
- 71
2 Answers
When to use barriers
Assume you have two TextView
widgets with dynamic heights and you want to place a Button
just below the tallest TextView
:
The ONLY 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"?>
<androidx.constraintlayout.widget.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" />
<androidx.constraintlayout.widget.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" />
</androidx.constraintlayout.widget.ConstraintLayout>
When to use guidelines
Assume you want to restrict the above-mentioned TextView
heights to 30% of screen height, no matter the content they have.
To implement that you should add horizontal Guideline
with percentage position and constrain the TextView
bottom to that Guideline
.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#AAA"
android:text="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
android:textSize="16sp"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintEnd_toStartOf="@+id/right_text_view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/right_text_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="#DDD"
android:text="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
android:textSize="16sp"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/left_text_view"
app:layout_constraintTop_toTopOf="parent" />
<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/guideline" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.3" />
</androidx.constraintlayout.widget.ConstraintLayout>
Conclusion
The only difference between Barrier
and Guideline
is that Barrier
's position is flexible and always based on the size of multiple UI elements contained within it and Guideline
's position is always fixed.

- 1,247
- 15
- 19

- 17,146
- 6
- 52
- 68
-
14a Valuable Answer! – Alireza Noorali Dec 01 '19 at 12:06
-
The claim: "The ONLY way to implement that directly in the layout is to use a horizontal Barrier." is false. You can use a guideline for this, by having both text boxes be constrained below by a guideline. (I.e. `app:layout_constraintBottom_toTopOf="@id/guideline"` – Chrispher Oct 30 '20 at 20:11
-
What happens to barrier when the visibility of reference views is gone. ? – pandey_shubham Sep 03 '21 at 12:51
-
3@pandey_shubham In that case the barrier is created on the resolved position of the GONE widget. If you do not want to take into account GONE widgets you can set `barrierAllowsGoneWidgets` to `false`. – Ionut Negru Nov 10 '21 at 09:23
-
Thank you very much for the explanation and example. It is very useful! – Oleksandr.D Apr 20 '22 at 08:57
-
I guess that by saying "ONLY" you refer on the same level, because you can easily do this by placing both views inside another layout with wrap content – htafoya Jul 20 '22 at 17:29
-
1For those whom this code doesn't show the barrier below the textviews, add app:layout_optimizationLevel="none" to the constraint layout. – Swati Pardeshi Nov 28 '22 at 12:59
Official documentation on Barrier:
A Barrier references multiple widgets as input, and creates a virtual guideline based on the most extreme widget on the specified side. For example, a left barrier will align to the left of all the referenced views.
Similar to a guideline, a barrier is an invisible line that you can constrain views to. Except a barrier does not define its own position; instead, the barrier position moves based on the position of views contained within it. This is useful when you want to constrain a view to the a set of views rather than to one specific view.

- 9,338
- 1
- 26
- 32