Xavier Rubio Jansana

Xavier Rubio Jansana

Mobile engineer.
Android and iOS.
Scuba diver.

© 2024

Autosizing TextView

In order to use Android O+ autosizing functionality in the TextView (also using AppCompat or AndroidX versions) there is a small important thing that needs to be taken into account: both dimensions must be constrained to a fixed size.

This means that wrap_content cannot be used in any of the dimensions. One could thought that having one of the dimensions fixed (e.g. the width) and the other one using wrap_content will use the constrained dimension to determine the scale of the text, and then the unconstrained (wrap_content) will match the calculated height, but that’s not the case: TextView tries to fit inside both dimensions always, so if one of them is unconstrained the result can range from text not being scaled (see this StackOverflow answer), to totally undefined (see Android Developer documentation). Specifically:

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.