Friday, September 16, 2016

Android Font Settings To Enable Font Variants

Today I learned that fonts often have settings to enable alternate representations of particular characters. For example Gotham is not a monospaced font.  However, if you enabled the "tnum" setting for your Android TextView, then the font will render as monospaced. That is cool!

It appears Android is supporting a W3 standard with this feature. The documentation has a link that references CSS Fonts. Furthermore, this method was added as part of API 21. So unfortunately your users on older API will not see the awesome column layout you can produce.

Android TextView Documentation

In code this would look something like this:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {    grandTotal.setFontFeatureSettings("tnum");}
or perhaps you are targetting API 21 and can apply the XML setting so that any string put into the field uses the "tnum" or other settings:

<TextView   
    android
:id="@+id/grand_total"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFeatureSettings="tnum"
    tools:text="$30.35"/>

Or perhaps you are targeting API 19 and your TextView has a style set that you can override in the values-21 directory:

<TextView
    android:id="@+id/three"
    style="@style/example"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="$20.13" />

Then you can define a base style configuration in values/styles.xml
<style name="example"> <item name="android:textSize">24sp</item> </style>
and then apply the fontFeatureSettings in values-v21/styles.xml
<style name="example"> <item name="android:textSize">24sp</item> <item name="android:fontFeatureSettings">tnum</item> </style>
I think the screenshot below with two emulators illustrates the difference well. You can clearly see that the columns do not line up between the three TextView fields.
On the right with "tnum"



1 comment:

john mash said...

Yeah great site i want to share the site where you can watch a lot of Movies,songs etc so let's visit https://showboxappguide.com//