Auto-Sizing TextViews in Android

They have come in Android Oreo but are supported upto Android 4.0 or later!

This article is part of Today I Learned series and was originally posted at my TIL Github Repository and my website at

Sometimes, we need our change its text size according to the content put in it. But, Android didn't have much support of it until Android O. And then Android Support library got this feature. Let's see how it all works.

First, you have to add the support library v26 or more in your file.

implementation ''
implementation ''

Although, it has been introduced in Android Oreo, but through using support library, this feature is backward compatible upto the Android 4.0 and later. Now, add a like you normally do.

android:text="Hello Any Small Text Goes Here"/>

Now, add this in your .

android:text="Hello Any Small Text Goes Here"/>

Now, your will behave according to the content inside it like this:

Regular TextView vs. Auto Sizing TextView

You can also do this from Java/Kotlin using this line

TextViewCompat.setAutoSizeTextTypeWithDefaults(myTextView, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);

Please note that if you set autosizing in an XML file, it is not recommended to use the value for the or attributes of a . It may produce unexpected results.

That’s not it. There are some more options as well. So far, we have only saw the default auto size text type, but there are two more ways to adjust text size as per our needs:

  1. Granularity
  2. Preset Sizes


In Granularity, The can uniformly scale, in a range between the given minimum and maximum size attributes. Each increment takes on the step size specified in the granularity attribute.

In XML, we can do it by adding these lines in our :


Or, in Java/Kotlin, we can do it by calling this method:

TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(TextView textview, int autoSizeMinTextSize, int autoSizeMaxTextSize, int autoSizeStepGranularity, int unit); 

// Example
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(myTextView, 12, 100, 2, TypedValue.COMPLEX_UNIT_SP);

Preset Sizes

By using preset sizes, you can autosize the from a list of predefined sizes.

In XML, Use the attribute in the layout file.


To access the array as a resource, define the array in the file.

<array name=”autosize_text_sizes”> 

Or you can do in Java/Kotlin like this:

TextViewCompat.setAutoSizeTextTypeUniformWithPresetSizes(int[] presetSizes, int unit);

// Example
int[] sizes = getContext().getResources().getIntArray(R.array.autosize_text_sizes)
TextViewCompat.setAutoSizeTextTypeUniformWithPresetSizes(myTextView, sizes, TypedValue.COMPLEX_UNIT_SP);

Wajahat Karim is an experienced android developer, an active open source contributor, and co-author of two books Learning Android Intents and Mastering Android Game Development with Unity. In his spare time, he likes to spend time with his family or do experiments on coding and open source, and loves to write about lots of things. He’s on Twitter @WajahatKarim.

Do you have a writing question you’d like him to answer? Contact him through his website at with DEAR WAJAHAT in the subject line.

🔥 Google Dev Expert (GDE) in Android . 📱 Android Dev . 💻FOSS Contributor . 📝Writer . 🎤 Speaker . 👶 Developer Dad — 🌐Subscribe at