Search code examples
androidtextviewandroid-xmlcenteringmultiline

How to center a multiline text view with the smallest width necessary?


I would like to have my 2-line text view in android-xml centered horizontally with the smallest width necessary. At the moment it's like the first line is completely filled and the second with the rest of the words. Does anyone know how to achieve that both lines are filled kind of equally? Below are 2 images, the first showing the default multi-line text view, the second what I would like to achieve. As you can see, the second image requires much less width than the first and is more in the center of the view.

Thanks in advance!

Current Text View

Wanted Text View


Solution

  • Try setting android:breakStrategy attribute of textView to balanced

    android:breakStrategy="balanced"
    

    Add this line to your textView's xml code, this will balance the lines.