Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Balanced Multiline TextView

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.

like image 678
mpkuth Avatar asked Oct 28 '15 16:10

mpkuth


1 Answers

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.

like image 148
mpkuth Avatar answered Sep 22 '22 02:09

mpkuth