1

I am using the following layout:

<?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">

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="start"
        app:constraint_referenced_ids="right_txt1,right_txt2,right_txt3"/>


    <TextView
        android:id="@+id/right_txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bar"
        android:gravity="end"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/left_txt1"
        app:layout_constraintBottom_toBottomOf="@id/left_txt1"
        />

    <TextView
        android:id="@+id/right_txt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bar Bar"
        android:gravity="end"
        app:layout_constraintTop_toBottomOf="@id/c1"
        app:layout_constraintEnd_toEndOf="parent"
        />

    <TextView
        android:id="@+id/right_txt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bar"
        app:layout_constraintTop_toBottomOf="@id/c2"
        app:layout_constraintEnd_toEndOf="parent"
        />


    <View
        android:id="@+id/view1"
        android:layout_width="120dp"
        android:layout_height="10dp"
        android:background="@color/green"
        app:layout_constraintTop_toTopOf="@id/right_txt1"
        app:layout_constraintBottom_toBottomOf="@id/right_txt1"
        app:layout_constraintEnd_toStartOf="@id/barrier"
        />

    <View
        android:id="@+id/view2"
        android:layout_width="120dp"
        android:layout_height="10dp"
        android:background="@color/green"
        app:layout_constraintTop_toTopOf="@id/right_txt2"
        app:layout_constraintBottom_toBottomOf="@id/right_txt2"
        app:layout_constraintEnd_toStartOf="@id/barrier"
        />

    <View
        android:id="@+id/view3"
        android:layout_width="120dp"
        android:layout_height="10dp"
        android:background="@color/green"
        app:layout_constraintTop_toTopOf="@id/right_txt3"
        app:layout_constraintBottom_toBottomOf="@id/right_txt3"
        app:layout_constraintEnd_toStartOf="@id/barrier"
        />

    <TextView
        android:id="@+id/left_txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="start"
        app:layout_constrainedWidth="true"
        app:layout_constraintTop_toTopOf="@id/view1"
        app:layout_constraintBottom_toBottomOf="@id/view1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/view1"
        app:layout_constraintHorizontal_bias="0"
        android:text="FooBar"
        />

    <androidx.constraintlayout.widget.Constraints
        android:id="@+id/c1"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        app:layout_constraintTop_toBottomOf="@id/left_txt1"
        app:layout_constraintStart_toStartOf="parent"
        />

    <TextView
        android:id="@+id/left_txt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constrainedWidth="true"
        app:layout_constraintTop_toTopOf="@id/view2"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/c1"
        app:layout_constraintBottom_toBottomOf="@id/view2"
        app:layout_constraintEnd_toStartOf="@id/view2"
        android:text="FooBarBaz"
        />

    <androidx.constraintlayout.widget.Constraints
        android:id="@+id/c2"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        app:layout_constraintTop_toBottomOf="@id/left_txt2"
        app:layout_constraintStart_toStartOf="parent"
        />

    <TextView
        android:id="@+id/left_txt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        app:layout_constraintTop_toTopOf="@id/view3"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/left_txt2"
        app:layout_constraintBottom_toBottomOf="@id/view3"
        app:layout_constraintEnd_toStartOf="@id/view3"
        android:text="FooBar"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

The result is:
enter image description here

This is ok for this example but what I want is that as the text grows the individual items adjust as a group without losing alignment. Examples that don't work properly:
enter image description here

and margins don't work
E.g. adding android:layout_marginBottom="18dp" to the left_txt1 has no effect or adding android:layout_marginBottom="18dp"to theleft_txt2` has the following effect

enter image description here

I would like as the text grows the items bellow/above give space as a group

What am I messing up here?

Jim
  • 3,845
  • 3
  • 22
  • 47

2 Answers2

1

For the left TextViews not to overlap each other you need to create vertical constraints between them. Then each progress bar and right TextView should be constrained according to height of the corresponding left TextView and not the other way around, as stated in the other answer.

<?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">

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="start"
        app:constraint_referenced_ids="right_txt1,right_txt2,right_txt3"
        />

    <TextView
        android:id="@+id/right_txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/left_txt1"
        app:layout_constraintBottom_toBottomOf="@id/left_txt1"
        />

    <TextView
        android:id="@+id/right_txt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bar Bar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/left_txt2"
        app:layout_constraintBottom_toBottomOf="@id/left_txt2"
        />

    <TextView
        android:id="@+id/right_txt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/left_txt3"
        app:layout_constraintBottom_toBottomOf="@id/left_txt3"
        />

    <View
        android:id="@+id/view1"
        android:layout_width="120dp"
        android:layout_height="10dp"
        android:background="@color/green"
        app:layout_constraintTop_toTopOf="@id/left_txt1"
        app:layout_constraintBottom_toBottomOf="@id/left_txt1"
        app:layout_constraintEnd_toStartOf="@id/barrier"
        />

    <View
        android:id="@+id/view2"
        android:layout_width="120dp"
        android:layout_height="10dp"
        android:background="@color/green"
        app:layout_constraintTop_toTopOf="@id/left_txt2"
        app:layout_constraintBottom_toBottomOf="@id/left_txt2"
        app:layout_constraintEnd_toStartOf="@id/barrier"
        />

    <View
        android:id="@+id/view3"
        android:layout_width="120dp"
        android:layout_height="10dp"
        android:background="@color/green"
        app:layout_constraintTop_toTopOf="@id/left_txt3"
        app:layout_constraintBottom_toBottomOf="@id/left_txt3"
        app:layout_constraintEnd_toStartOf="@id/barrier"
        />

    <TextView
        android:id="@+id/left_txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="start"
        app:layout_constrainedWidth="true"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/left_txt2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/view1"
        app:layout_constraintHorizontal_bias="0"
        android:text="FooBar"
        />

    <TextView
        android:id="@+id/left_txt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constrainedWidth="true"
        app:layout_constraintTop_toBottomOf="@id/left_txt1"
        app:layout_constraintBottom_toTopOf="@id/left_txt3"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/view2"
        app:layout_constraintHorizontal_bias="0"
        android:text="FooBarBaz"
        />

    <TextView
        android:id="@+id/left_txt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constrainedWidth="true"
        app:layout_constraintTop_toBottomOf="@id/left_txt2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/view3"
        app:layout_constraintHorizontal_bias="0"
        android:text="FooBar"
        />

</androidx.constraintlayout.widget.ConstraintLayout>
Pawel Laskowski
  • 6,078
  • 1
  • 21
  • 35
  • Thank you! What is the reasoning behind changing the direction of top/bottom like you did? Seems I did it in the reverse way but how should I be thinking about it in general? Also is this a case that a chain could have been used? – Jim Oct 26 '19 at 17:09
  • Left TextViews will be the ones determining the height of each horizontal group so other Views in that group should depend on the top/bottom of the left TextView. Now, left TextViews should also be constrainted to each other vertically because without these constraints they will overlap when their height increases. – Pawel Laskowski Oct 26 '19 at 19:12
  • 1
    The vertical chain could be used for the top/bottom constraints but it would spread the Views across all available space of the parent. To achieve the same desired effect as in my answer with a vertical chain you would have to add bottom constraint to the last left TextView, set the vertical bias of 0 to the chain and use packed chain style. – Pawel Laskowski Oct 26 '19 at 19:21
  • So if I understand correctly I can either adjust the group to the height of the left textview if that grows too big or the right textview. But not both? So if the right textviews are longer the alignment wouldn't be possible to be adjusted the same way as what is achieved now? – Jim Oct 26 '19 at 19:50
  • You could use a Barrier on the bottom of each horizontal group that would align to the bottom-most edge of all the Views in that group and then constraint the top of the next horizontal group to that Barrier and so on. – Pawel Laskowski Oct 26 '19 at 20:02
  • But if both left and right TextViews can grow big then the wrapping behavior of the text in both of them would be unpredictable. Everything I posted was assuming that only one of the TextViews will exceed the available space. – Pawel Laskowski Oct 26 '19 at 20:08
0

You should align the bar View following the heigh of TextView, not vice versa.

<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">

<androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="start"
app:constraint_referenced_ids="right_txt1,right_txt2,right_txt3"/>


<TextView
        android:id="@+id/right_txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bar"
        android:gravity="end"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/left_txt1"
        app:layout_constraintBottom_toBottomOf="@id/left_txt1"
/>

<TextView
        android:id="@+id/right_txt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bar Bar"
        android:gravity="end"
        app:layout_constraintTop_toBottomOf="@id/c1"
        app:layout_constraintEnd_toEndOf="parent"
/>

<TextView
        android:id="@+id/right_txt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bar"
        app:layout_constraintTop_toBottomOf="@id/c2"
        app:layout_constraintEnd_toEndOf="parent"
/>


<View
        android:id="@+id/view1"
        android:layout_width="120dp"
        android:layout_height="10dp"
        app:layout_constraintTop_toTopOf="@id/left_txt1"
        app:layout_constraintBottom_toBottomOf="@id/left_txt1"
        android:background="@color/yellow"
        app:layout_constraintEnd_toStartOf="@id/barrier"
/>

<View
        android:id="@+id/view2"
        android:layout_width="120dp"
        android:layout_height="10dp"
        android:background="@color/yellow"
        app:layout_constraintTop_toTopOf="@id/right_txt2"
        app:layout_constraintBottom_toBottomOf="@id/right_txt2"
        app:layout_constraintEnd_toStartOf="@id/barrier"
/>

<View
        android:id="@+id/view3"
        android:layout_width="120dp"
        android:layout_height="10dp"
        android:background="@color/yellow"
        app:layout_constraintTop_toTopOf="@id/right_txt3"
        app:layout_constraintBottom_toBottomOf="@id/right_txt3"
        app:layout_constraintEnd_toStartOf="@id/barrier"
/>

<TextView
        android:id="@+id/left_txt1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/view1"
        android:text="FooBar"
/>

<androidx.constraintlayout.widget.Constraints
        android:id="@+id/c1"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        app:layout_constraintTop_toBottomOf="@id/left_txt1"
        app:layout_constraintStart_toStartOf="parent"
/>

<TextView
        android:id="@+id/left_txt2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/left_txt1"
        app:layout_constraintEnd_toStartOf="@id/view2"
        android:text="FooBar"
/>

<androidx.constraintlayout.widget.Constraints
        android:id="@+id/c2"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        app:layout_constraintTop_toBottomOf="@id/left_txt2"
        app:layout_constraintStart_toStartOf="parent"
/>

<TextView
        android:id="@+id/left_txt3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/left_txt2"
        app:layout_constraintEnd_toStartOf="@id/view3"
        android:text="FooBar"
/>

</androidx.constraintlayout.widget.ConstraintLayout>
Duy Khanh Nguyen
  • 454
  • 3
  • 11
  • Tried that but didn't seem to work. Should I change something else too? – Jim Oct 26 '19 at 11:15
  • I have updated my answer, can you try it? And you should not align a side of a view with 2 other views. For example `app:layout_constraintTop_toTopOf` and `app:layout_constraintTop_toBottomOf` should not be used together. – Duy Khanh Nguyen Oct 27 '19 at 12:36