How to position below the lowest of two views in ConstraintLayout?

ZakTaccardi picture ZakTaccardi · Feb 21, 2017 · Viewed 20.2k times · Source

I have two header views, HeaderViewA and HeaderViewB. These views can have any combination of visibility visible or gone.

I need BigView to be positioned under the lowest of either HeaderViewA/HeaderViewB.

Is this possible without nesting in ConstraintLayout?

enter image description here

Answer

Eugene Brusov picture Eugene Brusov · Oct 5, 2017

Now it's possible with Barrier class, introduced in constraint-layout v1.1.0.

So here's the solution for your particular case:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.eugene.test10.MainActivity">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#EEEEEE"
        android:text="TEXT_VIEW_1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/textView2"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#DDDDDD"
        android:text="TEXT_VIEW_2"
        android:visibility="gone"
        app:layout_constraintLeft_toRightOf="@+id/textView1"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.constraint.Barrier
        android:id="@+id/labelBarrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="textView1,textView2" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#CCCC00"
        android:text="TEXT_VIEW_3"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/labelBarrier" />

</android.support.constraint.ConstraintLayout>

Here're results for using this layout:

View on simulator

You can refer this step-by-step guide on Codelab https://codelabs.developers.google.com/codelabs/constraint-layout/#10.