Monospace tabular numbers in Android TextViews
Asked Answered
U

1

19

I have a custom font which has variable-width numeric glyphs by default, and I would like to use the font's monospace tabular numbers feature in an Android TextView so that numbers align vertically.

That is, change something like this:

to something like this:

Ulric answered 14/12, 2016 at 13:1 Comment(0)
U
31

To select a font's tabular number feature, use TextView's fontFeatureSettings with tnum:

<TextView
    ...
    android:fontFeatureSettings="tnum"
    android:text="1111.11\n2222.22"
    />

Requires API level 21.

Ulric answered 14/12, 2016 at 13:1 Comment(3)
(Self-documenting something I learned today with no existing SO posts found.)Ulric
Or you can set it programmatically on the textview setFontFeatureSettings()Limemann
Thanks for sharing that @laalto. Here is how to do it using Jetpack Compose: https://mcmap.net/q/666649/-monospace-numbers-in-the-jetpack-compose-text-componentSchiro

© 2022 - 2024 — McMap. All rights reserved.