Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 339
Vishal Vyas Avatar asked Oct 11 '12 16:10

Vishal Vyas


People also ask

How to make multiline TextView in android?

The maxLines attribute is used to set the maximum line a TextView widget can have. You can prevent the TextView from creating multiple lines by setting the maxLines value as 1 . When you use the maxLines attribute, the full text of your TextView will be truncated, leaving a gap that may cause confusion.

What is multi line text in android?

TextView new line (multiline) TextView display text on android app. by default, TextView show text on a single line, and if it is long then TextView take more lines to display its text.


1 Answers

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")

like image 90
DeeV Avatar answered Oct 20 '22 05:10

DeeV