Home > Enterprise >  Android - Cannot get TextView to wrap text
Android - Cannot get TextView to wrap text

Time:11-09

I have a layout with some controls. I want a particular TextView to wrap text.
I have already search for google on how to to wrap text, but none worked for me.

Description
Find the Address control in the code bellow, which is the TextView with the long text.
Notice the Address control is placed in a LinearLayout. I was hoping the Address control wouldn't go outside the LinearLayout's layout.
When the TextView's text is long, the text goes behind the RightIcon control and continues forever to the right.

Question
How to cause the text to wrap and use a line bellow inside the LinearLayout an not go behind the RightIcon control?

Code (stripped from all non-necessary code)

<?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="wrap_content">

    <!-- Header -->
    <TextView
        android:id="@ id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <!-- LeftIcon -->
    <Button
        android:id="@ id/leftIcon"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:text="X"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/header"
        app:layout_constraintBottom_toBottomOf="parent" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginLeft="10dp"
        android:layout_marginVertical="15dp"
        app:layout_constraintStart_toEndOf="@id/leftIcon"
        app:layout_constraintTop_toBottomOf="@id/header"
        app:layout_constraintBottom_toBottomOf="parent" >

        <!-- Address -->
        <TextView
            android:id="@ id/address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize=20sp"
            tools:text="An address which is very long and must wrap to a line bellow" />

        <!-- Person 1 -->
        <TextView
            android:id="@ id/person1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:text="Person 1" />

        <!-- Person 2 -->
        <TextView
            android:id="@ id/person2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:text="Person 2" />

    </LinearLayout>

    <!-- RightIcon -->
    <Button
        android:id="@ id/rightIcon"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:text="Y"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/header"
        app:layout_constraintBottom_toBottomOf="parent" />
   </androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

In LinearLayout, you could add a connect to right icon:

app:layout_constraintEnd_toStartOf="@ id/rightIcon"

and change width of linear layout to "0dp".

  • Related