Posted on

Android Resizable TextView


android resizable textview

 

How about making your TextView change its text size automatically? Finally, Android support library got this feature. Let’s look at how to do that and in what cases you should

 

Setup

First, make sure that you’re using latest v 26 support library in your build.gradle

Use Cases

Ok, so in essence, you answer the question from Android: “What text size should this TextView have?” with

How should I know, you figure out

Even though it might seem like a simple task, it’s not. You lose some control over what your layout would look like

The first thing to consider – auto-resizable TextView doesn’t guarantee you displaying all the text. You might think

Hold on, Ihor, it’s auto-resizable, meaning test size will shrink down to fit all the text

Yes, it will, but you wouldn’t want it to shrink down to 8 sp or 4 sp, it will be unreadable, right? That’s why there’s a min text size attribute which will eventually crop your text if the string is too long

So the first question you need to answer is

Is displaying all text essential?

If no – you can use auto-resize feature, otherwise – just stick with fixed text size and wrap_context dimensions

Fixed Dimensions

The way it works is you set fixed dimensions for TextView. It won’t work with wrap_content, you can only use match_parent or hardcoded dp value. It gives some restrictions, but makes it easier to grasp the way it works

Titles

I think the best use case for auto-resizable TextView is titles. Because essentially what auto-resizable feature is for is to increase text size when possible. Otherwise, you’d just stick with fixed size

There isn’t much sense for making auto-resizable body TextViews where displaying all text isn’t guaranteed

Single Line

Let’s start with single line titles first

android resizable textview

 

Blue bounds show TextView dimensions. It has fixed 200dp height, as I mentioned above, dimensions should be fixed, for width in most cases it’s match_parent and some hardcoded value for height

autoSizeTextType of uniform enables auto-resizing. Pretty easy to add, hard to make it look great though! It tries to make text as big as possible in the constraints we set: width, height, maxLines, minTextSize (default 12sp), maxTextSize (default 122sp), granularity (step size, default 1sp)

So if text is short and fits fully with 12sp (default), it will increment text size with granularity(default 1sp) till it doesn’t fit and stop

Which means that if text doesn’t fit with 12sp – you have no way to shrink it, nor to increase height of TextView, even if it wasn’t set to 1 line max, hence hardcoded height dp

And basically, you can control resulting text size with just height of TextView

android resizable textview

Ellipsize

Here’s a useful attribute, if you have a long text or undefined text that changes and you might get this

android resizable textview

This may be way too small, let’s set min text size

 

android resizable textview

As you see, some words are cut out. But you can fix with setting

android resizable textview

This looks better, once again, if you set hardcoded TextView dimensions, which are required for auto-resizing, you should expect words missing out sometimes

Any Number Of Lines

Once you’ve learned a bit how TextView width/height, min/max text size attributes behave you know what to expect from multiline then. It’s way more complex than it seems. WYGIIGNI, stands for What You Get Is I’ve Got No Idea!

android resizable textview

Downsides

Here are few downsides that I’ve noticed. If there’s another view below or above your auto-resizable one – it might lead to ugly layout

 

android resizable textview

This is caused by auto-resizable TextView having one line max and having a redundant height of 200dp. You could always set TextView gravity attribute in those cases though

Recap

So it’s great that finally, you don’t need to copy code from StackOverflow for auto-resizable TextView and what attributes you got

  • autoSizeTextType
  • granularity
  • autoSizeMinTextSize
  • autoSizeMaxTextSize

Hint: attribute autocomplete Ctrl + Space filles in android: namespase, replace with app:

Don’t forget to subscribe, follow me on Facebook, Twitter and G+ to get notified about latest posts!