View on the right only if their is enough space, otherwise go at the bottom
Asked Answered
S

2

5

Using the ConstraintLayout I have 2 TextView that I want to be next to each other when their is enough space to see the content of both. But if TextView A takes a lot of space and fullfill all the screen width, than I would like that TextView B goes under TextView A instead of still being on the right but invisible (out of the screen).

So with the code below I got my 2 TextView right next to each other.

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/text_view_a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        tools:text="a very long text ..." />

    <TextView
        android:id="@+id/text_view_b"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintLeft_toRightOf="@+id/text_view_a"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="some text" />

</android.support.constraint.ConstraintLayout>

How should I edit this code snippet so that TextView B goes under TextView A if their isn't any space left for him?

Singly answered 30/5, 2017 at 9:10 Comment(0)
M
6

Take a look at Google's FlexboxLayout. It may be what you need to flow your TextViews. You can find some documentation here and a tutorial here.

FlexboxLayout is a library project which brings the similar capabilities of CSS Flexible Box Layout Module to Android.

You can look at wrapping your TextViews in the FlexbotLayout within the ConstraintLayout to allow them to flow as needed.

Here is a sample layout. The two TextViews will be stacked due to the long text of the top TextView. If you shorten this text, the two TextViews will be side-by-side.

I hope this helps.

<?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.flexer.MainActivity">

    <com.google.android.flexbox.FlexboxLayout 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="0dp"
        app:alignContent="stretch"
        app:alignItems="stretch"
        app:flexWrap="wrap"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/text_view_a"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:text="a very long text ...a very long text ...a very long text ...a very long text ...a very long text ...a very long text ...a very long text ...a very long text ..." />

        <TextView
            android:id="@+id/text_view_b"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:text="some text" />
    </com.google.android.flexbox.FlexboxLayout>
</android.support.constraint.ConstraintLayout>
Mychal answered 30/5, 2017 at 18:33 Comment(0)
C
2

I just had to add this answer since as much as the accepted answer works,id recommend a different approach using the currently existing constraint layout of which its usage is highly recommended due to UI performance advantages.its as simple as adding a androidx.constraintlayout.helper.widget.Flow and setting the reference id of the views to flow:

<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>
Clements answered 16/1, 2022 at 11:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.