8

I am starting with this:

<TextView
    android:text="Here is what it looks like"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:gravity="center"/>

Which renders like this:

| Here is what it looks |
|         like          |

But I want it to render like this:

|      Here is what     |
|     it looks like     |

Is there a way to do this without customizing the built in TextView? If not, how can TextView be customized to do this?

Note that the android:text value is actually filled in programmatically and can vary in content/length. I am looking for a general solution that works for any text value on any screen width.

double-beep
  • 5,031
  • 17
  • 33
  • 41
mpkuth
  • 6,994
  • 2
  • 27
  • 44

2 Answers2

15

Starting in API 23, TextView has break strategy and hyphenation frequency settings that can be used to achieve this.

<TextView
    android:text="Here is what it looks like"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:hyphenationFrequency="none"
    android:breakStrategy="balanced"
    android:gravity="center"/>

However, I don't see any way to do this for API <23. AppCompatTextView in version 23.1.0 of the support library doesn't seem to support these.

NOTE: Android Studio 1.4.1 does not show the desired wrapping behavior in the preview window (even when API 23 is selected for the preview), but it can be seen in an emulated API 23 device.

mpkuth
  • 6,994
  • 2
  • 27
  • 44
  • 3
    I have filed two bugs on this: [request to backport on the support library](https://code.google.com/p/android/issues/detail?id=203863) and [hyphenation only works in English](https://code.google.com/p/android/issues/detail?id=203895). – Benoit Duffez Mar 15 '16 at 07:17
1

Or you can just layout_alignparent_left with gravity center.