Android
TextView
Auto-fit
UI Development
Mobile App Development

Auto-fit TextView for Android

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

An auto-fit TextView adjusts its text size so content stays readable without overflowing its bounds. This is useful for labels that must survive different screen sizes, translations, and unpredictable user data.

Modern Android already supports text autosizing, so most projects do not need a custom view or a third-party library for this behavior.

Using Built-In Autosizing In XML

The simplest approach is to enable autosizing directly in layout XML. With AndroidX or recent platform support, you can define a minimum text size, maximum text size, and the size step the framework should try.

xml
1<?xml version="1.0" encoding="utf-8"?>
2<androidx.appcompat.widget.AppCompatTextView
3    xmlns:android="http://schemas.android.com/apk/res/android"
4    android:id="@+id/titleText"
5    android:layout_width="match_parent"
6    android:layout_height="wrap_content"
7    android:maxLines="2"
8    android:text="A long headline that should shrink if necessary"
9    android:autoSizeTextType="uniform"
10    android:autoSizeMinTextSize="12sp"
11    android:autoSizeMaxTextSize="24sp"
12    android:autoSizeStepGranularity="1sp" />

uniform means Android tries different text sizes within the allowed range until the content fits.

Configuring Autosize In Code

You can also configure autosizing programmatically. This is useful when the size range depends on runtime state:

kotlin
1import androidx.appcompat.app.AppCompatActivity
2import android.os.Bundle
3import androidx.core.widget.TextViewCompat
4import com.example.app.databinding.ActivityMainBinding
5
6class MainActivity : AppCompatActivity() {
7    private lateinit var binding: ActivityMainBinding
8
9    override fun onCreate(savedInstanceState: Bundle?) {
10        super.onCreate(savedInstanceState)
11        binding = ActivityMainBinding.inflate(layoutInflater)
12        setContentView(binding.root)
13
14        TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(
15            binding.titleText,
16            12,
17            24,
18            1,
19            android.util.TypedValue.COMPLEX_UNIT_SP
20        )
21
22        binding.titleText.text = "Text that resizes to stay inside the available width"
23    }
24}

This achieves the same effect as the XML attributes while keeping the configuration in code.

When Autosize Works Well

Autosizing is a good fit for titles, badges, counters, or short status messages where the layout size is fixed but the text length varies. It is less suitable for long paragraphs because shrinking a lot of prose usually hurts readability more than wrapping or scrolling.

So the question is not only whether text can fit. It is whether making the text smaller still produces a good user experience.

Important Layout Settings

Autosizing depends on real layout constraints. If the TextView has effectively unlimited width, the text will not need to shrink. In practice, you usually combine autosize with one or more of these settings:

  • a fixed or constrained width
  • a maximum line count
  • ellipsize behavior for edge cases
  • padding that reflects the actual design

Without realistic layout constraints, the autosize settings may appear to do nothing.

A Practical Example

Here is a layout where the text must fit inside a narrow card header:

xml
1<androidx.appcompat.widget.AppCompatTextView
2    android:id="@+id/cardTitle"
3    android:layout_width="0dp"
4    android:layout_height="wrap_content"
5    android:layout_marginStart="16dp"
6    android:layout_marginEnd="16dp"
7    android:maxLines="1"
8    android:ellipsize="end"
9    android:autoSizeTextType="uniform"
10    android:autoSizeMinTextSize="10sp"
11    android:autoSizeMaxTextSize="22sp"
12    android:autoSizeStepGranularity="1sp" />

Because the width is constrained, Android has a clear target and can reduce the text size when a title becomes too long. In list items such as RecyclerView rows, it is worth testing this with real translated strings and accessibility font scales.

Common Pitfalls

The biggest mistake is treating autosize as a cure-all for poor layout design. If the text block is too small to remain readable, shrinking the font only hides the design problem.

Another pitfall is combining autosize with unconstrained width. If the view can expand freely, the autosize engine has no reason to reduce the font size.

A third issue is setting the minimum size too low. The text may technically fit, but if it becomes unreadable on smaller devices, the feature is not helping.

Summary

  • Android supports auto-fit text natively through text autosizing.
  • XML attributes are the simplest way to enable it.
  • 'TextViewCompat lets you configure the same behavior in code.'
  • Autosize works best for short dynamic text inside constrained layouts.
  • Choose sensible minimum sizes so fitting text does not come at the cost of readability.

Course illustration
Course illustration

All Rights Reserved.