Android O allows you to 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 Beta provides full support to the autosizing
TextView feature on devices running Android versions
prior to Android O. 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:
Default
Default setting lets the autosizing of TextView scale
uniformly on horizontal and vertical axes.
- To define the default setting programmatically, call the
setAutoSizeTextTypeWithDefaults(@AutoSizeTextType int autoSizeTextType)method. ProvideAUTO_SIZE_TEXT_TYPE_NONEto turn off the autosizing feature orAUTO_SIZE_TEXT_TYPE_UNIFORMto scale the horizontal and the vertical axes uniformly. - To define the default setting in XML, use the
androidnamespace and set theautoSizeTextTypeattribute to none or uniform.
Note: The default dimensions for uniform
scaling are minTextSize = 12sp,
maxTextSize = 112sp, and granularity = 1px.
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" 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 theTextViewwidget and one of the text types, such asTextViewCompat.AUTO_SIZE_TEXT_TYPE_NONEorTextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM. - To define the default setting in XML through the support library, use the
appnamespace and set theautoSizeTextTypeattribute to none or uniform.
<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="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:autoSizeTextType="uniform"
/>
/>
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 anyTypedValuedimension unit. - To define a range of text sizes and a dimension in XML, use the
androidnamespace and set the following attributes:- Set the
autoSizeTextattribute to either none or uniform. none is a default value and uniform letsTextViewscale uniformly on horizontal and vertical axes. - Set
autoSizeMinTextSize,autoSizeMaxTextSize, andautoSizeStepGranularityattributes to define the dimensions for the autosizing ofTextView.
- Set the
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" 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 anyTypedValuedimension unit. - To define a range of text sizes and a dimension in XML through the support
library, use the
appnamespace and setautoSizeText,autoSizeMinTextSize,autoSizeMaxTextSize, andautoSizeStepGranularityattributes in the layout XML file.
<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="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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
TextViewprogrammatically, call thesetAutoSizeTextTypeUniformWithPresetSizes(int[] presetSizes, int unit)method. Provide an array of sizes and anyTypedValuedimension unit for the size. -
To use preset sizes to set up the autosizing of
TextViewin XML, use theandroidnamespace and set the following attributes:- Set the
autoSizeTextattribute to either none or uniform. none is a default value and uniform letsTextViewscale uniformly on horizontal and vertical axes. - Set the
autoSizePresetSizesattribute to an array of preset sizes. To access the array as a resource, define the array in theres/values/arrays.xmlfile.
- 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>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:autoSizeTextType="uniform" android:autoSizePresetSizes="@array/autosize_text_sizes" />
Using support library
- To use preset sizes to set up the autosizing of
TextViewprogrammatically through the support library, call theTextViewCompat.setAutoSizeTextTypeUniformWithPresetSizes(TextView textView, int[] presetSizes, int unit)method. Provide an instance of theTextViewclass, an array of sizes, and anyTypedValuedimension unit for the size. - To use preset sizes to set up the autosizing of
TextViewin XML through the support library, use theappnamespace and setautoSizeTextandautoSizePresetSizesattributes 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>
<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="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:autoSizeTextType="uniform"
app:autoSizePresetSizes="@array/autosize_text_sizes"
/>
</LinearLayout>