Milack27
Milack27

Reputation: 1689

TextView: wrap content in multiple lines

I have the following TextView:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:minLines="2"
    android:text="Lorem ipsum dolor sit amet"/>

This is the behavior I get:

This is the behavior I expected:

I'd like to get this behavior without having to set a hardcoded width, because the text is different for each language. Is there any way to do it?

EDIT: I'd also like to avoid binding the width of the TextView to the width of the parent. It doesn't matter if there's enough space to display the whole text in a single line, I want it displayed in two lines. Given that, I'd like the TextView to have the minimum possible width.

Upvotes: 4

Views: 4441

Answers (1)

Tamir Abutbul
Tamir Abutbul

Reputation: 7661

Yes, you can use ConstraintLayout with Guidelines to determine your textView width (not hard coded), here is an simple example without using guidlines (there are constraints on the text view) :

<?xml version="1.0" encoding="utf-8"?>
  <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/mainQuestionsLayout"
  android:layout_width="match_parent"
  android:layout_height="match_parent">


<TextView
    android:id="@+id/textView11"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:text="TextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdw"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

It will look like this:

enter image description here

Now, if you want to have more flexible width you can use guidelines or just give your text view constraints to another view and set its width to 0dp

Here is an example with guideline:

<?xml version="1.0" encoding="utf-8"?>
<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/mainQuestionsLayout"
  android:layout_width="match_parent"
  android:layout_height="match_parent">


<TextView
    android:id="@+id/textView11"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:text="TextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdwTextVieasdasdw"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/guideline19"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

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

And it will look like this:

enter image description here

Please note that the dotted line is the actual guideline, this what it looks like on the design preview, you won't see the guideline on the run time but your textView will look like this.

Upvotes: 1

Related Questions