I have a horizontal LinearLayout with 3 text views with width set to wrap_content. I want them all to be wrap content, but there is a potential that one of them becomes too long. This results in the other two text views become out of bound. How can I make the long text view wrap content only to an extend where all children of LinearLayout are still there (within the boundary of the width of the parent LinearLayout (set to match_parent))?

Code sample:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="32dp"
    android:orientation="horizontal"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toBottomOf="parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a very very very very very very very very very long text"
        android:ellipsize="end"
        android:maxLines="1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is another text"
        android:maxLines="1"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="One final text"
        android:ellipsize="end"
        android:maxLines="1"/>
</LinearLayout>

This is what I get: undesired

This is more of what I want: desired

NOTE: android:layout_weight didn't work for me because I still want the text view to wrap content for smaller texts.

5

There are 5 best solutions below

3
creg On

Set width of the parent (LinearLayout) to wrap_content. However this will cause the second text view to come on top of the first textview if text of first textview exceeds the start of the second text view.

Best option i see is specify layout_width for all the text views and then set the max length of text for each textview.

2
javdromero On

You can set a android:maxWidth and android:layout_width="wrap_content" per TextView

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="32dp"
    android:orientation="horizontal"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toBottomOf="parent">
    <TextView
        android:layout_width="wrap_content"
        android:maxWidth="150dp"
        android:layout_height="wrap_content"
        android:text="This is a very very very very very very very very very long text"
        android:ellipsize="end"
        android:maxLines="1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxWidth="150dp"
        android:text="This is another text"
        android:maxLines="1"/>
    <TextView
        android:layout_width="wrap_content"
        android:maxWidth="150dp"
        android:layout_height="wrap_content"
        android:text="One final text"
        android:ellipsize="end"
        android:maxLines="1"/>
</LinearLayout>

enter image description here

5
AudioBubble On

In your LinearLayout change the layout weights:

     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="32dp"
        android:orientation="horizontal"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@color/teal_200"
            android:text="This is a very very very very very very very very very long text"
            android:ellipsize="end"
            android:maxLines="1"/>

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@color/teal_700"
            android:text="This is another text"
            android:maxLines="1"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@color/purple_500"
            android:text="One final text"
            android:ellipsize="end"
            android:maxLines="1"/>
    </LinearLayout>

1
AudioBubble On

This seemed to work

    <?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="32dp"
        android:orientation="horizontal"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@color/teal_200"
            android:text="This is a very very very very very very very very very long text"
            android:ellipsize="end"
            android:maxLines="1"/>

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@color/teal_700"
            android:text="This is another text This is a very very very very very very very very very long text"
            android:maxLines="1"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@color/purple_500"
            android:text="One final text This is a very very very very very very very very very long text"
            android:ellipsize="end"
            android:maxLines="1"/>
    </LinearLayout>


</androidx.constraintlayout.widget.ConstraintLayout>


enter image description here

1
labetao On

at the linearLayout add this parameter:

android:weightSum="3"

at EACH of the 3 TextViews set WIDTH to zero like this:

android:layout_width="0dp"

And add weight of 1 like this:

android:layout_weight="1"

if you have 2 "TextViews", then the "weightSum" should be 2, if you have 4 then the "weightSum" should be 4.

This will make it distribute the layout evenly.