android autosize textview
| | |

AutoSize TextView Android

Finally, Android O support library got this feature covered. Material design recommends using a dynamic type instead of smaller type sizes or truncating larger size text. A TextView that automatically resize the text, expand and fill its layout based on the text view characteristics and boundaries. These characteristics make it easier to optimize the text size with different screen size and dynamic content.

Auto-resizing can also help you avoid empty spaces in your layout or text that gets cut off mid-sentence because you tried to add too many words into a TextView.

Dependency

For to use Autosize TextView you need to add a dependency in your build.gradle file.

implementation 'com.android.support:appcompat-v7:27.1.0'
Types of setting Autosize TextView
  • Default
  • Granularity
  • Preset Sizes
1. Default

To define the default settings of Autosizing TextView with xml set “setautoSizeTextType” to uniform. Let’s see an example how we can use it in the xml file.

<TextView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:autoSizeTextType="uniform" />

Now this setting of TextView scale uniformly on horizontally and vertically ignoring the text size attributes. If you’re not settings the minTextSize, maxTextSize, and granularity properties then the default values will be used 12sp, 112sp, and 1px respectively.

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.

You can also set Autosizing of a TextView programmatically.

emojiCompatTextView.setAutoSizeTextTypeWithDefaults(TextView.AUTO_SIZE_TEXT_TYPE_UNIFORM)
2. Granularity

With Granularity, the TextView will scale uniformly in the range between minimum and maximum size in increments of step granularity. If you don’t set these properties the default values will be used.

To implement Autosizing using granularity you’ll need to add the following attributes in the xml file. Below is the example how to use granularity in xml.

<TextView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:autoSizeTextType="uniform"
    app:autoSizeMinTextSize="12sp"
    app:autoSizeMaxTextSize="100sp"
    app:autoSizeStepGranularity="2sp" />

You can also set Autosizing using granularity programmatically.

emojiCompatTextView.setAutoSizeTextTypeUniformWithConfiguration(12, 100, 2, TextView.AUTO_SIZE_TEXT_TYPE_UNIFORM)
3. Preset Sizes

To have more control over the final size. e.g your app needs to compile with specific text size design guidelines. You can provide an array of supported text sizes and it will use the largest one that fits. All you need to do for Preset Sizes create an array in your resources and then set “autoSizePresetSizes” in xml.

<TextView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:autoSizeTextType="uniform"
    app:autoSizePresetSizes="@array/autosize_text_sizes" />

An array of resources.

<array name="autosize_text_sizes">
    <item>12sp</item>
    <item>24sp</item>
    <item>36sp</item>
    <item>48sp</item>
    <item>60sp</item>
    <item>72sp</item>
</array>

You can also set Autosizing using Preset Sizes programmatically.

emojiCompatTextView.setAutoSizeTextTypeUniformWithPresetSizes(resources.getIntArray(R.array.auto_size_array),TextView.AUTO_SIZE_TEXT_TYPE_UNIFORM)

Alright guy’s, that’s my explanation about Autosizing TextView with programmatically and with xml. If you guys have any query about this post please do comment below.

Thank you for being here and keep reading.

Similar Posts

4 Comments

  1. Excelente post.
    Claro, presiso y funcional, de verdad que era justo lo que buscaba y me fue de gran ayuda, excelente mi hermano.

  2. After I initially commented I seem to have clicked on the -Notify me when new comments are added- checkbox and
    from now on each time a comment is added I receive four emails with the same
    comment. There has to be a way you are able to
    remove me from that service? Kudos!

Comments are closed.