Posted by & filed under Android, Java, Tipps & Tricks.

I was in need of a TextView that has a fixed with but dynamic content, so I needed the content to automatically scale.

The AutoScaleTextView takes the android:textSize value and uses it as the preferred text size. You can also set the minimum text size. The default for the minimum text size is 10dp. The AutoScaleTextView now tries to take the maximum value between the preferred and minimum size that fits into the Views width (with regarding the padding).

Here is the result:

The AutoScale TextView in action displaying the android version names

You can see that the AutoScaleTextView automatically scales the text to fit in the Views width. For the different views I used layout_width: 130dp and layout_height=30dp, textSize: 16dp and 10dp for the minTextSize. On the second Ice Cream Sandwich TextView I used 13dp for the minTextSize and layout_height: wrap_content.

Get the source and explanation after the break.

The code itself is pretty simple. I improved a snippet I found on I sadly cannot remember the exact post.

You can checkout the demo project:

Or download the project:

Please feel free to leave feedback or report bugs here:

Or view the code below:

The important part is the refitText(String, int) method. Note: If you are dealing with big textsizes you should set the treshold to a higher value.

Here is a snippet from the main.xml I used in the demo project

The values from the res/values/dimens.xml are 100dp for the width and 30dp for the height.

The content of the res/values/attrs.xml to alter the minTextSize in the layout files is:


And the content of the res/values/styles.xml to set the default minTextSize or other default styles for the AutoScaleTextView


Have fun using the code. I uploaded the code under the Beerware License. You can take the code and do whatever you want with it, as long as you mention me somewhere in your code, or your final app. If we ever meet and you think that the code, or explanation was helpful, feel free to buy me a beer and have a chat.

Or you could help a fellow out by donating some money.

Updated licence

When I uploaded this code I never thought anyone would use it, but since some people actually are using it, I updated the licence to Apache 2.0. But still… If you’re in the mood to buy me a beer. Please do :)

Comments are closed.