0

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.

lama
  • 31
  • 3

4 Answers4

1

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.

creg
  • 13
  • 4
  • the problem is that the max width varies according to the visibility of the second and third text views. – lama Jul 29 '21 at 14:15
  • set width to "wrap_content" but do an if condition in the activity class to change layout_width to a specific size if text length exceeds a specific number. – creg Jul 29 '21 at 14:24
  • I have updated the answer, just change the layout weights and to make this possible adjust the widths to 0dp –  Jul 29 '21 at 14:27
1

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

javdromero
  • 1,850
  • 2
  • 11
  • 19
0

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>

0

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