Vishal Vyas
Vishal Vyas

Reputation: 2581

Android TextView with multiple lines

I want a TextView that should be broken into 4 lines. For e.g.

Vishal Vyas
  Having
   342
Reputation

Note, the gravity should be center_horizontal

I tried following :

<TextView
    android:gravity="center_horizontal"
    android:id="@+id/lblUserRep"
    android:layout_width="70dp"
    android:layout_height="wrap_content"
    android:lines="4"
    android:maxLines="4"
    android:text="VishalVyas Having 342 Reputation" >
</TextView>

This works! but produces following output:

VishalVyas
  Having
   342
Reputation

Problems:

  1. It doesn't work with the space between words Vishal and Vyas.
  2. android:layout_width="70dp" is harcoded and there can be any name with n number of characters instead of VishalVyas.

Please advice.

Added: It would be fine if I need to write a custom TextView for achieving this but I'll require some guidance.

Thanks in advance.

Upvotes: 20

Views: 76752

Answers (4)

Vicente Domingos
Vicente Domingos

Reputation: 99

I did so:

The Container of TextView:

    android:layout_width="match_parent"
    android:layout_height="match_parent"

The TextView:

    android:layout_width="match_parent"
    android:layout_height="wrap_content"

Then,

The Text of the TextView was showed in two lines or more...

Upvotes: 1

display name
display name

Reputation: 899

android:lines="2"
android:minLines="2"
android:singleLine="false"

Even if Android Studio warns that android:singleLine=false is deprecated, keep it and one can have the number of lines they want for their text box depending on the length of their text

Upvotes: 8

DeeV
DeeV

Reputation: 36035

I think it's wrapping because "Vishal Vyas" is going beyond 70dp. Instead, do wrap_content on the width and use newline characters for lines instead of wrapping (i.e. "Vishal Vyas\n342\nReputation")

Upvotes: 23

Blumer
Blumer

Reputation: 5035

You should be able to insert the newline character \n to control where the splits go. Once you do that, you can expand your TextView wider so that it can accommodate a longer user name but still break in the right place.

Upvotes: 14

Related Questions