Google is committed to advancing racial equity for Black communities. See how.

With Android 8.0 (API level 26) and higher, you can instruct a TextView to let the text size expand or contract automatically to fill its layout based on the TextView 's characteristics and boundaries. This setting makes it easier to optimize the text size on different screens with dynamic content.

The Support Library 26.0 provides full support to the autosizing TextView feature on devices running Android versions prior to Android 8.0 (API level 26). The library provides support to Android 4.0 (API level 14) and higher. The android.support.v4.widget package contains the TextViewCompat class to access features in a backward-compatible fashion.

Setting up TextView autosize

You can use either framework or support library to set up the autosizing of TextView programmatically or in XML. To set the XML attributes, you can also use the Properties window in Android Studio.

There are three ways you can set up the autosizing of TextView :

Note: 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.

Default

Default setting lets the autosizing of TextView scale uniformly on horizontal and vertical axes.

To define the default setting programmatically, call the setAutoSizeTextTypeWithDefaults(int autoSizeTextType) method. Provide AUTO_SIZE_TEXT_TYPE_NONE to turn off the autosizing feature or AUTO_SIZE_TEXT_TYPE_UNIFORM to scale the horizontal and the vertical axes uniformly.

method. Provide to turn off the autosizing feature or to scale the horizontal and the vertical axes uniformly. Note: The default dimensions for uniform scaling are minTextSize = 12sp , maxTextSize = 112sp , and granularity = 1px. To define the default setting in XML, use the android namespace and set the autoSizeTextType attribute to none or uniform.

<?xml version="1.0" encoding="utf-8"?> <TextView android:layout_width="match_parent" android:layout_height="200dp" android:autoSizeTextType="uniform" />

Using support library

To define the default setting programmatically through the support library, call the TextViewCompat.setAutoSizeTextTypeWithDefaults(TextView textview, int autoSizeTextType) method. Provide an instance of the TextView widget and one of the text types, such as TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE or TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM .

method. Provide an instance of the widget and one of the text types, such as or . To define the default setting in XML through the support library, use the app namespace and set the autoSizeTextType attribute to none or uniform.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="200dp" app:autoSizeTextType="uniform" /> </LinearLayout>

Granularity

You can define a range of minimum and maximum text sizes and a dimension that specifies the size of each step. The TextView scales uniformly in a range between the minimum and maximum size attributes. Each increment occurs as per the step size set in the granularity attribute.

To define a range of text sizes and a dimension programmatically, call the setAutoSizeTextTypeUniformWithConfiguration(int autoSizeMinTextSize, int autoSizeMaxTextSize, int autoSizeStepGranularity, int unit) method. Provide the maximum value, the minimum value, the granularity value, and any TypedValue dimension unit.

method. Provide the maximum value, the minimum value, the granularity value, and any dimension unit. To define a range of text sizes and a dimension in XML, use the android namespace and set the following attributes: Set the autoSizeText attribute to either none or uniform. none is a default value and uniform lets TextView scale uniformly on horizontal and vertical axes. Set autoSizeMinTextSize , autoSizeMaxTextSize , and autoSizeStepGranularity attributes to define the dimensions for the autosizing of TextView .

namespace and set the following attributes:

<?xml version="1.0" encoding="utf-8"?> <TextView android:layout_width="match_parent" android:layout_height="200dp" android:autoSizeTextType="uniform" android:autoSizeMinTextSize="12sp" android:autoSizeMaxTextSize="100sp" android:autoSizeStepGranularity="2sp" />

Using support library

To define a range of text sizes and a dimension programmatically through the support library, call the TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(int autoSizeMinTextSize, int autoSizeMaxTextSize, int autoSizeStepGranularity, int unit) method. Provide the maximum value, the minimum value, the granularity value, and any TypedValue dimension unit.

method. Provide the maximum value, the minimum value, the granularity value, and any dimension unit. To define a range of text sizes and a dimension in XML through the support library, use the app namespace and set autoSizeText , autoSizeMinTextSize , autoSizeMaxTextSize , and autoSizeStepGranularity attributes in the layout XML file.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="200dp" app:autoSizeTextType="uniform" app:autoSizeMinTextSize="12sp" app:autoSizeMaxTextSize="100sp" app:autoSizeStepGranularity="2sp" /> </LinearLayout>

Preset Sizes

Preset sizes lets you specify all the values that the TextView picks when automatically auto-sizing text.

To use preset sizes to set up the autosizing of TextView programmatically, call the setAutoSizeTextTypeUniformWithPresetSizes(int[] presetSizes, int unit) method. Provide an array of sizes and any TypedValue dimension unit for the size.

programmatically, call the method. Provide an array of sizes and any dimension unit for the size. To use preset sizes to set up the autosizing of TextView in XML, use the android namespace and set the following attributes: Set the autoSizeText attribute to either none or uniform. none is a default value and uniform lets TextView scale uniformly on horizontal and vertical axes. Set the autoSizePresetSizes attribute to an array of preset sizes. To access the array as a resource, define the array in the res/values/arrays.xml file.

in XML, use the namespace and set the following attributes:

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

<?xml version="1.0" encoding="utf-8"?> <TextView android:layout_width="match_parent" android:layout_height="200dp" android:autoSizeTextType="uniform" android:autoSizePresetSizes="@array/autosize_text_sizes" />

Using support library

To use preset sizes to set up the autosizing of TextView programmatically through the support library, call the TextViewCompat.setAutoSizeTextTypeUniformWithPresetSizes(TextView textView, int[] presetSizes, int unit) method. Provide an instance of the TextView class, an array of sizes, and any TypedValue dimension unit for the size.

programmatically through the support library, call the method. Provide an instance of the class, an array of sizes, and any dimension unit for the size. To use preset sizes to set up the autosizing of TextView in XML through the support library, use the app namespace and set autoSizeText and autoSizePresetSizes attributes in the layout XML file.

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

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="200dp" app:autoSizeTextType="uniform" app:autoSizePresetSizes="@array/autosize_text_sizes" /> </LinearLayout>

Additional resources