Jbwz
Jbwz

Reputation: 305

How to force TextView to use as much space as possible on the first line

I have a problem with creating a chat layout.

How can I force TextView's text to fill up the entire area? Even if the string is continuous, sometimes text goes to the next line.

Problem

XML with TextView:

<?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_marginBottom="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/my_msg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/message_my_shape"
    android:paddingLeft="16dp"
    android:paddingTop="8dp"
    android:paddingRight="16dp"
    android:paddingBottom="8dp"
    android:text="Hey mate,how youre doing?"
    android:textColor="#fcc7d3"
    android:textSize="16sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    tools:layout_editor_absoluteY="0dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

UPD: You don't quite get me, guys. Padding should be there and I don't want a single line, please see the new screenshots with layout boundaries shown to see what I'm talking about. Question is still the same, why text in TextView doesn't fill all available space (sometimes)? enter image description here

enter image description here

Upvotes: 1

Views: 1093

Answers (2)

Jbwz
Jbwz

Reputation: 305

Found a solution.

For API 23 and higher add to your TextView:

android:breakStrategy="simple"

For API < 23 remove hidden &nbsp; from you text dynamically (android adds them automatically).

Upvotes: 3

Andr&#233; Sousa
Andr&#233; Sousa

Reputation: 1730

This reason why this happens is that you didn't specify that your TextView only has one line.

From the documentation:

android:maxLines

Makes the TextView be at most this many lines tall. When used on an editable text, the inputType attribute's value must be combined with the textMultiLine flag for the maxLines attribute to apply.

So if you want to always have a single line of text use:

android:maxLines=1

Anything this to your code:

<TextView
    android:id="@+id/my_msg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/message_my_shape"
    android:paddingLeft="16dp"
    android:paddingTop="8dp"
    android:paddingRight="16dp"
    android:paddingBottom="8dp"
    android:text="Hey mate,how youre doing?"
    android:textColor="#fcc7d3"
    android:textSize="16sp"
    android:maxLines=1
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    tools:layout_editor_absoluteY="0dp" />

Upvotes: -1

Related Questions