4

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.

Milack27
  • 1,619
  • 2
  • 20
  • 31

1 Answers1

1

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.

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
  • Thank you for your answer! But I also don't want to bind the width of the TextView to the width of the parent. What I want is the TextView to have the minimum possible width provided that the text is displayed in two lines (it's acceptable that the text is displayed in more than two lines in case there's not enough space). I wasn't clear at all, sorry about that. – Milack27 May 03 '19 at 15:30
  • You don't have to bind the width of the TextView to the width of the parent, check the part of the answer that talking about guidelines. if you want to tell your textView - be 2 line that's something else and I would recommend you to do it from your code actually – Tamir Abutbul May 03 '19 at 15:49
  • I understand, but every guideline is defined in terms of a distance from the parent's borders (absolute or percentage). So when you're binding the width of the TextView to a guideline, you're indirectly binding it to the width of the parent. – Milack27 May 03 '19 at 17:37
  • In that case, I don't think that you have any other way to do it other than making a fixed size to your view. OR - you can get your textViews text, split it in half and aff `\n` in the middle of it. The result will be 2 line textView – Tamir Abutbul May 04 '19 at 07:34
  • The \n solution is exactly what I ended up doing. :D – Milack27 May 04 '19 at 12:16
  • Glad it helped, if my answer was helpful please accept it so it could help other developers with the same problem in the future. – Tamir Abutbul May 04 '19 at 12:19