Autosize TextViews

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 text size on different screens with dynamic content.

Support Library 26.0 fully supports the autosizing TextView feature on devices running Android versions 8.0 (API level 26) or lower. The android.support.v4.widget package contains the TextViewCompat class to access features in a backward-compatible fashion.

Set up TextView autosize

You can either use the 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, described in the sections that follow:

Note: If you set autosizing in an XML file, we do not recommended using the value "wrap_content" for the layout_width or layout_height attributes of a TextView. Doing so might produce unexpected results.

Default

The 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.
  • 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" />

Define the default setting using the 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.
  • 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 the step size set in the granularity attribute.

<?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" />

Define granularity using the Support Library

<?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 let you specify the values that the TextView picks when autosizing 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.
  • To use preset sizes to set up the autosizing of TextView in XML, use the android namespace and set the following attributes:
    • Set the autoSizeTextType attribute to either none or uniform. The none value is the default, 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.
<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" />

Set up preset sizes using the 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.
  • To use preset sizes to set up the autosizing of TextView in XML through the Support Library, use the app namespace and set the autoSizeTextType 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

For additional information on autosizing a TextView when working with dynamic content, watch Android Jetpack: Autosizing TextView.