18

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:

laalto
  • 150,114
  • 66
  • 286
  • 303

1 Answers1

28

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.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • 2
    (Self-documenting something I learned today with no existing SO posts found.) – laalto Dec 14 '16 at 13:02
  • 2
    Or you can set it programmatically on the textview setFontFeatureSettings() – Joakim Mar 07 '19 at 12:18
  • Thanks for sharing that @laalto. Here is how to do it using Jetpack Compose: https://stackoverflow.com/a/68995057/2521749 – amenon Aug 31 '21 at 15:53