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. ProvideAUTO_SIZE_TEXT_TYPE_NONE
to turn off the autosizing feature orAUTO_SIZE_TEXT_TYPE_UNIFORM
to scale the horizontal and the vertical axes uniformly. - To define the default setting in XML, use the
android
namespace and set theautoSizeTextType
attribute to none or uniform.
Note: The default dimensions for uniform
scaling are minTextSize = 12sp
,
maxTextSize = 112sp
, and granularity = 1px.
<?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 theTextView
widget and one of the text types, such asTextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE
orTextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM
. - To define the default setting in XML through the support library, use the
app
namespace and set theautoSizeTextType
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 anyTypedValue
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 letsTextView
scale uniformly on horizontal and vertical axes. - Set
autoSizeMinTextSize
,autoSizeMaxTextSize
, andautoSizeStepGranularity
attributes to define the dimensions for the autosizing ofTextView
.
- Set the
<?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 anyTypedValue
dimension unit. - To define a range of text sizes and a dimension in XML through the support
library, use the
app
namespace and setautoSizeText
,autoSizeMinTextSize
,autoSizeMaxTextSize
, andautoSizeStepGranularity
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 thesetAutoSizeTextTypeUniformWithPresetSizes(int[] presetSizes, int unit)
method. Provide an array of sizes and anyTypedValue
dimension unit for the size. -
To use preset sizes to set up the autosizing of
TextView
in XML, use theandroid
namespace and set the following attributes:- Set the
autoSizeText
attribute to either none or uniform. none is a default value and uniform letsTextView
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 theres/values/arrays.xml
file.
- Set the
<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 theTextViewCompat.setAutoSizeTextTypeUniformWithPresetSizes(TextView textView, int[] presetSizes, int unit)
method. Provide an instance of theTextView
class, an array of sizes, and anyTypedValue
dimension unit for the size. - To use preset sizes to set up the autosizing of
TextView
in XML through the support library, use theapp
namespace and setautoSizeText
andautoSizePresetSizes
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 Autosizing TextView.