Auto-Sizing TextViews in Android

Sometimes, we need our TextView 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 build.gradle file.

implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:support-v4:26.1.0'

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 TextView like you normally do.

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:text="Hello Any Small Text Goes Here"/>

Now, add this app:autoSizeTextType="uniform" in your TextView.

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    app:autoSizeTextType="uniform"
    android:text="Hello Any Small Text Goes Here"/>

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

Auto Sizing TextView Examples

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 "wrap_content" for the layout_width or layout_height attributes of a TextView. It may produce unexpected results.

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

  1. Granularity
  2. Preset Sizes

Granularity

In Granularity, The TextView 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 TextView:

    app:autoSizeMaxTextSize=”100sp” 
    app:autoSizeMinTextSize=”12sp” 
    app:autoSizeStepGranularity=”2sp” 
    app:autoSizeText=”uniform” 

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 TextView from a list of predefined sizes.

In XML, Use the autoSizePresetSizesautoSizePresetSizes attribute in the layout file.

    android:autoSizeText=”uniform” 
    android:autoSizePresetSizes=”@array/autosize_text_sizes” 

To access the array as a resource, define the array in the res/values/arrays.xml file.

<array name=”autosize_text_sizes”> 
    <item>10sp</item> 
    <item>12sp</item> 
    <item>20sp</item> 
    <item>40sp</item> 
    <item>100sp</item> 
</array>

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);

If you liked this article, you can read my new articles below:


Wajahat Karim is Pakistan’s first Google Developer Expert in Android. As an experience Android developer, he deeply cares about it and keeps writing and speaking about it. He has written two worldwide 300+ pages books on Android development with more than 100 articles around the internet either on his website or his Medium publications. He is also a passionate contributor in open source and has created many Android libraries used by thousands of developers in their apps worldwide. As active public speaker, he spends lots of time giving talks in conferences and motivating people on Android development. You can find Wajahat most active on Twitter @WajahatKarim where he regularly shares all the good stuff about Android and community building.

Author's picture

Wajahat Karim

🔥 Google Dev Expert (GDE) in Android .
📱 Android Dev. 💻 Open Source Contributor . 📝 Technical Writer . 🎤 Public Speaker

Senior Android Developer

Karachi, Pakistan