32

I have read in a blog entry that in ConstraintLayout 2.0.0, a way was introduced to create a flow of views. What I exactly want to achieve: I have several TextViews with a fixed size next to each other. Depending on screen size, some TextViews should be pushed into the next line.

Example on big screen with six TextViews:

[AAA] [BBB] [CCC] [DDD]

[EEE] [FFF]

Example on small screen with six TextViews:

[AAA] [BBB]

[CCC] [DDD]

[EEE] [FFF]

I already saw this Stackoverflow question proposing to use a FlexboxLayout, but there is a comment saying that the same thing now can be achieved using ConstraintLayout.

Anybody can give me an example on how to achieve the desired behavior using ConstraintLayout? I was not able to find any instructions about this. Thanks in advance.

Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
BenjyTec
  • 1,719
  • 2
  • 12
  • 22
  • I see that flow is mentioned in this [video](https://www.youtube.com/watch?v=W39H7972buY) by the developers at around time mark 26:18, but I don't see where it has been actually released. – Cheticamp Feb 26 '19 at 01:53

1 Answers1

56

You can achieve this by adding a androidx.constraintlayout.helper.widget.Flow virtual view inside the androidx.constraintlayout.widget.ConstraintLayout and by referencing all your textviews with app:constraint_referenced_ids attribute.

Example below shows how I've achieved it.

<androidx.constraintlayout.widget.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"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="[AAAAAAAA]"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="[BBBBBBBB]"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="[CCCCCCCC]"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@+id/text4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="[DDDDDDDD]"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@+id/text5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="[EEEEEEEE]"
        tools:ignore="MissingConstraints" />

    <TextView
        android:id="@+id/text6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="[FFFFFFFF]"
        tools:ignore="MissingConstraints" />

    <androidx.constraintlayout.helper.widget.Flow
        android:id="@+id/flow1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="10dp"
        app:constraint_referenced_ids="text1,text2,text3,text4,text5,text6"
        app:flow_horizontalBias="0"
        app:flow_horizontalGap="10dp"
        app:flow_horizontalStyle="packed"
        app:flow_verticalBias="0"
        app:flow_wrapMode="chain"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Play around with app:flow_ attributes to change the arrangement of the textviews such as alignment, margin, etc. https://developer.android.com/reference/androidx/constraintlayout/helper/widget/Flow

Final result should look like below depending on your screen size.

Result

Yasitha Waduge
  • 13,180
  • 5
  • 35
  • 42
  • 4
    I think in this case the wrap_aligned for the wrapMode is good as that will turn it into columns https://developer.android.com/reference/android/support/constraint/helper/Flow#wrap_aligned – Josttie Jun 03 '19 at 09:22
  • @Yasitha Waduge and if we want to add a background to this flow ? Is possible ? – aeroxr1 Dec 15 '21 at 11:03
  • @aeroxr1 Yes you can add a background to the constraint layout. (Though flow is a virtual layout and I think adding a background to the flow wouldn't have any effect) – Yasitha Waduge Dec 15 '21 at 21:39