Autosizing TextView Tutorial for Android: Getting Started [FREE]


Autosizing TextView Tutorial for Android: Getting Started [FREE]

Android has released new properties for TextView which let you automatically resize text depending on the width and/or height of your text area. In this autosizing TextView tutorial, you』ll learn how to use this cool new feature.

Mobile apps use TextViews extensively. Sometimes, the developer has to use a fixed height or width, which makes resizing the font to always display the text challenging.

Starting with Android 8.0 (API level 26), you can set these new autosizing properties with the TextView class. Thanks to the Support Library 26.0, you can also use these on devices running Android 4.0 (API level 14) or later by using the TextViewCompat class.

Note: This tutorial assumes you are familiar with the basics of Android and Kotlin. If you are new to Android Development, check out our Android Tutorial for Beginners series . Just need to catch up on Kotlin? Take a look at Kotlin For Android: An Introduction

Getting Started

The project you』ll work with, Jokes , is an app that shows you a list of jokes. You can pick a joke to create an image with, add other quotes, change the background and share the result with your friends.

Use the Download Materials button at the top or bottom of this tutorial to download the starter project.

Once downloaded, open the starter project in Android Studio 3.2.1 or newer. Build and run it, and you』ll see the following screen:

Autosizing TextView Tutorial for Android: Getting Started [FREE]

Selecting one of the jokes brings you to a new screen that shows only the joke you chose. You can add more text and customize the background. You can also move and pinch the TextView s. When you have finished customizing the page, you can share it with your friends.

Autosizing TextView Tutorial for Android: Getting Started [FREE]

You may have noticed that much of the text in the List Screen is sometimes cut off. Also, resizing the text in the Detail Screen doesn』t adjust the font size to the frame of the TextView .

Autosizing TextView Tutorial for Android: Getting Started [FREE]

To fix these problems, you』ll need to modify the code to autosize the TextView s according to their width and height.

Review the project to familiarize yourself with the files:

  • JokeDetailActivity.kt : Shows a single joke and lets you add additional text, customize the background and share.
  • joke_item.xml : Is the layout for each of the list items in the List Screen
  • activity_detail.xml : Is the layout for the Detail Screen.

Configuring TextView Autosizing Properties

There are three options that you need to consider when configuring the Autosize properties: AutoSizeTextType , Granularity and Preset Sizes . You』ll learn about them in this section.


If you want to enable autosizing, you need to set the app:autoSizeTextType property to uniform in the xml. Otherwise, the default is none , which disables this autosizing feature.

Open joke_item.xml , which the List Screen uses, and change the emoji TextView with id @+id/emoji to include this attribute. It should now look like the following:

Notice that you have added app:autoSizeTextType="uniform" to enable autosizing.

Note : If you are using a minSdk lower than 26 in your own project when using this, you may see a warning. To get rid of it, change from TextView to .

Now, following the same pattern, change the joke TextView with id @+id/jokeText to add app:autoSizeTextType="uniform" . It should now look like this:

Open activity_detail.xml and change the joke description TextView with the id @+id/jokeDescription the same way to be the following:

Build and run the application. You』ll notice that the TextView s in the List Screen and the Detail Screen now autosize correctly.

Autosizing TextView Tutorial for Android: Getting Started [FREE]

Autosizing TextView Tutorial for Android: Getting Started [FREE]

However, if you try to add a TextView in the Detail Screen, it won』t autosize when you pinch the view because it』s added at runtime.

To fix this, open JokeDetailActivity.kt and find the buildTextView() method. Replace the call to setTextSize(TypedValue.COMPLEX_UNIT_SP, 24f) with the following:


You』ve replaced the hardcoded font size of 24sp with the uniform AutoSizeTextType , but this time programmatically.

Note : For the autosizing to work properly, it』s important to set the width and the height to either match_parent or to a specific value in dps , either in xml or programmatically. Using wrap_content is not recommended because it may produce unexpected results.

Build and run the app again. Select a joke, add some additional text and resize the TextView . It should now scale properly.

Autosizing TextView Tutorial for Android: Getting Started [FREE]


If you want to customize the text further, you can set a range by configuring a minimum , maximum and step increment so that the TextView scales uniformly within the range.

Suppose your UX team has design guidelines that specify that the List Screen should have text with a minimum of 12sp and a maximum of 28sp with steps of 4sp.

Open joke_item.xml and change the joke TextView with the id @+id/jokeText to include the following:


Build and run the application and select a joke. What happens when you change the size of the joke text?

You should also make this change programmatically when new TextView s are added. Replace the call to setAutoSizeTextTypeWithDefaults() you added in buildTextView() with the following:

    12, 28, 4,

Preset Sizes

Instead of using minimum, maximum and a step increment, you can specify preset values that the TextView can use when autosizing text.

Create an arrays.xml file under app ▸ src ▸ main ▸ res ▸ values with this content:


Here you are using the same step increments as before, except that you can use whatever values you want.

Open joke_item.xml and change the joke TextView to replace this:


with this:


To make this change programmatically, change that same line in setAutoSizeTextTypeWithDefaults() to:

        intArrayOf(12, 16, 20, 24, 28),

You can also get the values from the xml file you created by using context.resources.getIntArray(R.array.autosize_text_sizes) instead of creating the array manually with intArrayOf() .

Build and run the app and play with resizing the TextView . Try changing the values in the array, then building and running the app again to see how resizing the TextView affects the scaling.

Where to Go From Here?

Congratulations! You』ve just learned how to use the autosizing TextView properties on Android.

You can download the final version of the project using the Download Materials button at the top or bottom of this tutorial.

Here are some great references to learn more about the subject:

If you have any questions or additional tips to share, please join in the forum discussion below!

原文 : Ray Wenderlich

免责声明:本文内容来源于Ray Wenderlich,已注明原文出处和链接,文章观点不代表立场,如若侵犯到您的权益,或涉不实谣言,敬请向我们提出检举。