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 wajahatkarim.com

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 '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 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 in your .

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

Granularity

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 :

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

In XML, Use the 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 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);

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 wajahatkarim.com with DEAR WAJAHAT in the subject line.

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