Autosizing TextView Tutorial for Android: Getting Started | raywenderlich.com

In this Android Autosizing TextView tutorial, you’ll learn how to use new TextView properties to build an app with text that autosizes, adapting dynamically to changes in height and width.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/7930-autosizing-textview-tutorial-for-android-getting-started

Thanks for the tutorial!:grinning:
But the programmatically AutoSizing is working on my device.:thinking:
I test with the Final project, same result, the additional text can’t resize…
I’m try to solve it but fail, anyone have the same issue??
(Android 8, API 26)

Not sure which one is not working for you. However, to make it work programmatically, please make sure you’re not setting any autosizing properties in the xml file.

I’ll give an example for you to try:

In the Final project, open joke_item.xml and remove the following properties from the jokeText TextView:

app:autoSizePresetSizes="@array/autosize_text_sizes"
app:autoSizeTextType="uniform"

Now run the app, you should see that all the joke items have the same text size.

Next, open JokesAdapter.kt and add the following to the bind method:

    fun bind(position: Int, listener: Listener?) {
      TextViewCompat.setAutoSizeTextTypeWithDefaults(jokeTextView,
              TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM)
    ...

Run the app again, the joke items should autosize their text sizes.

Thank you so much for your reply.
Yes, I try your method, in this way, the programmatically Text AutoSizing is Working.:grinning:
But, I found that programmatically Text AutoSizing(TextViewCompat.setAutoSizeTextTypeWithDefaults) is not work when the Text View is create programmatically, ex in code, ( return TextView(this).apply{…})

Thanks.:grin:

Make sure that the TextView that you are creating programmatically has the width and the height set to either match_parent or to a specific value in dps, as explained in the tutorial.

For example, in the final project, open JokeDetailActivity and check that in buildTextView method the width and height are set.

If you just want to enable autosizing to this TextView, you could also replace this:

      TextViewCompat.setAutoSizeTextTypeUniformWithPresetSizes(this,
          intArrayOf(12, 16, 20, 24, 28),
          TypedValue.COMPLEX_UNIT_SP)

with the following:

      TextViewCompat.setAutoSizeTextTypeWithDefaults(this,
              TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM)