pawelzieba
pawelzieba

Reputation: 16082

Uniform text wrapping in TextView

I need nice text wrapping in TextView, especially for text in headers.

Text wrapping for TextView might look like this, where the last word is in new line:

| ========================= |
|          =====            |

That what I would like to have is wrapping where lines width is more equable:

|     ================      |
|      ==============       |

It's easy to add '\n' for one language and test it on different screen sizes but not when there is more than 10 translations.

Upvotes: 4

Views: 2712

Answers (6)

Morgan Koh
Morgan Koh

Reputation: 2465

Check out Android's own autoTextSizing for TextView, added in add API 26 with Support Library that supports from API 14 onwards with app:autoSizeTextType="uniform".

Have fun!

https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview.html

Upvotes: 0

pawelzieba
pawelzieba

Reputation: 16082

I have modified TextView and created UniformTextView. After investigating of TextView sources I have decided to minimize TextView's width to have preferred lines number.

    <pl.dziobas.uniformtextview.UniformTextView
        android:text="@string/sample_text"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        app:prefLineNumber="2"
        style="@style/sample_style" />

It works satisfactorily for me.
UniformTextView example

Sources are available on github

Upvotes: 3

MaciejG&#243;rski
MaciejG&#243;rski

Reputation: 22232

The algorithm would be roughly:

  1. calculate String width (with Paint.measureText or something)
  2. if it is less than container (TextView) width, just use it
  3. otherwise divide String width by container to know how many "\n" to enter
  4. with some search algorithm look for a space character the closest to 1/nth of the width (again using measureText if the font is not monospace)
  5. substring at that point and repeat point 4 for n-1 (n > 1)

Probably will require some adjustments like using container width by some small percent smaller than real.

Upvotes: 1

Amit Gupta
Amit Gupta

Reputation: 8939

There is an open source project in Github AutoFittextView and in BitBucket AutoScaletextView.

You can change according to your requirement.

I tried the AutoScaleTextview and reached to the below OutPut.

enter image description here

Hope this will help you.

Upvotes: 1

Vivek Soneja
Vivek Soneja

Reputation: 880

If you know whats the text in advance (I means if its a hard coded text), you can fix the width of TextView in dp, so that it wraps up properly, or else you may also use android:ems to fix the maximum number of characters in a line. So your TextView can be:

<TextView
   android:layout_height="wrap_content"
   android:layout_width="100dp"
   android:lines="2"/>

OR

  <TextView
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:ems="15"
     android:lines="2" />

I hope this helps!

Upvotes: 0

Patrick
Patrick

Reputation: 35232

You can add a '\n' in your string resouce xml to add a newline so you can managethe wrapping yourself.

Another approach would be to dynamically add the '\n' where you get the string length divided by 2 and search for the next space in either direction and on the first find you just add '\n' there. A hack, but probably work.

Other than that there is not much in Android for Hyphenation or Typography. Propably this post will give you some tips: http://smarter-than-the-average-pierre.blogspot.co.at/2011/10/bad-android-typography-tale-of-text.html

Upvotes: 1

Related Questions