Autosize a multiline textview in a ConstraintLayout

1.4k views Asked by At

I'm trying to get a TextView which could be multiple lines tall and with the minimal width needed large. I tried a wrap_content width, but the TextView stays on a single line and overlaps parent if the text is too long. Then, I put the width to 0dp, and layout_constraintWidth_default to wrap. That's almost perfect, but if the text is too long for a single line, the TextView takes all the remaining space.

Here's what I have and what I'd like to have :

Multine TextView

And the xml :

<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:id="@+id/clMatchRow"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp">

  

    <TextView
        android:id="@+id/tvHomeTeam"
        style="@style/DefaultTheme.MatchTextview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|end"
        android:padding="5dp"
        android:text="ThisTime not veryShort"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/tvHomeScore"
        app:layout_constraintHorizontal_bias="1"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_default="wrap" />

    <TextView
        android:id="@+id/tvHomeScore"
        style="@style/DefaultTheme.MatchTextview"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:gravity="center_vertical|end"
        android:padding="5dp"
        android:text="5"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/guidelineHome"
        app:layout_constraintHorizontal_bias="1"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toEndOf="@id/tvHomeTeam"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.constraint.Guideline
        android:id="@+id/guidelineHome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.45" />

    <android.support.constraint.Guideline
        android:id="@+id/guidelineAway"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.55" />

    <TextView
        android:id="@+id/tvAwayScore"
        style="@style/DefaultTheme.MatchTextview"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:gravity="center_vertical|start"
        android:padding="5dp"
        android:text="2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/tvAwayTeam"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="@id/guidelineAway"
        app:layout_constraintTop_toTopOf="parent" />


    <TextView
        android:id="@+id/tvAwayTeam"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal|start"
        android:padding="5dp"
        android:text="Short"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toEndOf="@id/tvAwayScore"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_default="wrap" />

</android.support.constraint.ConstraintLayout>
1

There are 1 answers

1
Abhimanyu On

For TextView with id=tvHomeTeam

Set:

android:layout_width="wrap_content"
android:textAlignment="viewEnd"