I'm looking for an optimal way to resize wrapping text in a TextView
so that it will fit within its getHeight and getWidth bounds. I'm not simply looking for a way to wrap the text- I want to make sure it both wraps and is small enough to fit entirely on the screen.
I've seen a few cases on StackOverflow where auto resizing was needed, but they are either very special cases with hack solutions, have no solution, or involve re-drawing the TextView
recursively until it is small enough (which is memory intense and forces the user to watch the text shrink step-by-step with every recursion).
But I'm sure somebody out there has found a good solution that doesn't involve what I'm doing: writing several heavy routines that parse and measure the text, resize the text, and repeat until a suitably small size has been found.
What routines does TextView
use to wrap the text? Couldn't those be somehow used to predict whether text will be small enough?
tl;dr: is there a best-practice way to auto-resize a TextView
to fit, wrapped, in its getHeight and getWidth bounds?
I have use code from chase and M-WaJeEh and I found some advantage & disadvantage here
from chase
from M-WaJeEh
This solutions works for us:
Warning, bug in Android Honeycomb and Ice Cream Sandwich
Androids versions: 3.1 - 4.04 have a bug, that setTextSize() inside of TextView works only for the 1st time (1st invocation).
Bug is described here: http://code.google.com/p/android/issues/detail?id=22493 http://code.google.com/p/android/issues/detail?id=17343#c9
workaround is to add new line character to text assigned to TextView before changing size:
I use it in my code as follow:
I add this "\u3000" character on left and right of my text, to keep it centered. If you have it aligned to left then append to the right only. Of course it can be also embedded with AutoResizeTextView widget, but I wanted to keep fix code outside.
UPDATE: Following code also fulfills the requirement of an ideal AutoScaleTextView as described here : Auto-fit TextView for Android and is marked as winner.
UPDATE 2: Support of maxlines added, now works fine before API level 16.
Update 3: Support for
android:drawableLeft
,android:drawableRight
,android:drawableTop
andandroid:drawableBottom
tags added, thanks to MartinH's simple fix here.My requirements were little bit different. I needed an efficient way to adjust size because I was animating an integer from, may be 0 to ~4000 in
TextView
in 2 seconds and I wanted to adjust the size accordingly. My solution works bit differently. Here is what final result looks like:and the code that produced it:
And finally the java code:
I needed a specific solution. I have got an edittext and textview in my layout. The textview is fixed height and width. When the user starts to type in the edittext, the text should immediately appear in the textview. The text in the textfield should auto - resize to fit the textview. So I updated Chase's solution to work for me. So when the text changes in the textview, resizing starts. The difference between mine and Chase's soluton: resizing is done even if the user DELETE some chars. I hope it can help someone.
Here is the approach I take. It's very simple. It uses successive approximation to zero in on the fontsize and can generally have it figured out in less than 10 iterations. Just replace "activityWidth" with the width of whatever view you are using to display the text in. In my example, it's set as a private field to the screen's width. The inital fontsize of 198 is only set in the event the method generates an exception (which really should never happen):