IBlackVikingl
IBlackVikingl

Reputation: 1243

TextView break long text into new lines

I have a small TextView inside CardView like design. It used with as Firebase Recycler View to display content. Here its 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"
    android:layout_marginStart="8dp"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="8dp"
    android:background="@drawable/main_design">

    <ImageView
        android:id="@+id/rycImage"
        android:layout_width="100dp"
        android:layout_height="180dp"
        android:layout_marginStart="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp"
        android:scaleType="centerCrop"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/rycName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:ellipsize="end"
        android:justificationMode="inter_word"
        android:lines="1"
        android:text="Name"
        android:textColor="@color/white"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/rycImage"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/rycDesc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="8dp"
        android:ellipsize="end"
        android:justificationMode="inter_word"
        android:inputType="textMultiLine"
        android:maxLines="4"
        android:lines="4"
        android:text="Desc"
        android:textColor="@color/white"
        android:textSize="14sp"
        app:layout_constraintBottom_toTopOf="@+id/rycPhone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/rycImage"
        app:layout_constraintTop_toBottomOf="@+id/rycName" />

    <TextView
        android:id="@+id/rycMail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="8dp"
        android:text="e-mail"
        android:textColor="@color/white"
        android:textSize="14sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/rycImage" />

    <TextView
        android:id="@+id/rycPhone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:text="phone"
        android:textColor="@color/white"
        android:textSize="14sp"
        app:layout_constraintBottom_toTopOf="@+id/rycMail"
        app:layout_constraintEnd_toEndOf="@+id/rycMail"
        app:layout_constraintStart_toStartOf="@+id/rycMail" />
</androidx.constraintlayout.widget.ConstraintLayout>

TextView called rycDesc is a long text and i want it to break into new line if it reached its margin left and right. Right now its going beyond its constrains and over ImageView. How can i make it break line after it reaches his contrains?

enter image description here

Upvotes: 1

Views: 1972

Answers (5)

Embydextrous
Embydextrous

Reputation: 1661

Since rycDesc's left and right edges are constrained you can give it a width of 0dp so that these constraints are honored instead of wrap_content

Just change:

<TextView
        android:id="@+id/rycDesc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"

to

<TextView
        android:id="@+id/rycDesc"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"

This is how it looks now:

enter image description here

If you are using a newer version of ConstraintLayout you can keep wrap_content and add this to rycDesc:

app:layout_constrainedWidth="true"

Upvotes: 2

Harshit Gupta
Harshit Gupta

Reputation: 94

Use this line inside Your TextView in xml

android:singleLine="false"

Upvotes: 0

Hosein Haqiqian
Hosein Haqiqian

Reputation: 699

<TextView
    android:id="@+id/rycDesc"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="16dp"
    android:layout_marginBottom="8dp"
    android:ellipsize="end"
    android:justificationMode="inter_word"
    android:inputType="textMultiLine"
    android:maxLines="2"
    android:text="Desc"
    android:textColor="@color/white"
    android:textSize="14sp"
    app:layout_constraintBottom_toTopOf="@+id/rycPhone"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/rycImage"
    app:layout_constraintTop_toBottomOf="@+id/rycName" />

Upvotes: 2

Mhb flh
Mhb flh

Reputation: 31

you can use 2 LinearLayout(Horizental) instead ,one for your picture and one for all your text

Upvotes: 0

Elango
Elango

Reputation: 422

replace with

<TextView
        android:id="@+id/rycDesc"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:ellipsize="end"
        android:maxLines="4"
        android:text="Desc"
        android:textColor="@color/white"
        android:textSize="14sp"
        app:layout_constraintBottom_toTopOf="@+id/rycPhone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/rycImage"
        app:layout_constraintTop_toBottomOf="@+id/rycName" />

Upvotes: 0

Related Questions