Android Jetpack: Autosizing TextView
Articles Blog

Android Jetpack: Autosizing TextView

August 22, 2019

user experience with text, Material Designer recommends
using a dynamic type, instead of smaller type sizes or
truncating larger size text. We made this much easier to
implement with the introduction of TextView AutoSizing. Let’s see how to use
it, how to customize it, and what caveats you
need to be aware of. With Android O and
Jetpack, text view gained a new property,
auto size text type, which allows you to
optimize the text size when working with dynamic content. To enable auto sizing, set
auto size text type to uniform. This scales text uniformly on
horizontal and vertical axes, ignoring the text
size attribute. When using Jetpack, make sure
you use the app namespace. Note that you should not use
wrap content for layout width or layout height for a
text view set to auto size, since, well, it may
produce unexpected results. Instead use match
parent or a fixed size. Turn off auto
sizing by selecting none instead of uniform. You can also use auto size
programmatically like this. If you want to customize
your text view more, we have some extra
attributes for you. Auto size min and max, text
size, and step granularity. The text view will
scale uniformly in the range between the
minimum and the maximum size in increments of
step granularity. If you don’t set
these properties, the default values will be used. Programmatically these values
can be set via this method. To have more control
over the final size, if, for example, your app needs
to comply with specific text size design guidelines, you
can provide a list of sizes, and it will use the
largest one that fits. Create an array with the
sizes in your resources, and then set the auto size
preset sizes attribute in the XML. Or do this in code
by providing an array of preset sizes and their unit. Using dynamic sizes
for text views makes your app more interesting
and helps your users decode content quickly. So start using the
auto size property. Fine tune it as you see fit. And improve your
user experience. [SOARING]

Only registered users can comment.

  1. Is this one of the ways to solve the fontSize issue on bigger screen device? Traditionally dev just specifically set the X sp for all font.

  2. Why Google never support lower android version, but some dev always find a way to create a lib that support every API?

    This is like WTF.

  3. I use ConstraintLayout and it success do all autosize very nice. Why I need "autoSizeTextType" property?

  4. I understand Google likes to show the developers presenting their own work, and this is really awesome, but it can be hard to understand them when the developer have a strong accent, maybe not to an American, but for people around the world it's a lot harder. Please provide subtitles or only show an American developer.

  5. This is just supported in API 26 right? The majority of devices are pobably below this API level, so how to do that for lower APIs?

  6. My normal process results in textviews with width of wrap_content. I would be curious about strategies to rework layouts where match_parent makes sense.

  7. Hey can anyone explain me? I've seen a lot of videos talking about Jetpack and that is great and so but when can I use it? how can I use it? is it compatible with my old proyects? what Do I need to start using it?

  8. And still, this auto-resizing mechanism is broken, after so much time that I've reported about it:
    Even on the current video, you can see this issue, on 0:24 . The font can be resized to be smaller, as you can see on 0:30, but instead of it, you can see the text doing word wrapping or even character wrapping, which looks really bad.

  9. It's not the best solution. If you want single row TextView to scale down the text to fit inside it's not the best. When you have TextView with width=match_parent, height=wrap_content, maxLines=1 and you want the text to fit in that, it scales down the textSize but the view height is not changed so it results in empty space below the text. So if you have views below this TextView it looks like you have margin at the bottom and it makes the UI not like you wanted it. I see that you suggest fixed size, but I always try not to use fixed sizes, because I want the layout to look good on every device

Leave a Reply

Your email address will not be published. Required fields are marked *