What's happened to Monospace in Android Lollipop?
Asked Answered
M

1

11

I have a style to use "monospace" in my Android App:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Base application theme. -->
    <style name="AppTheme"  parent="Theme.AppCompat.Light.DarkActionBar" >
        <!-- Customize your theme here. -->
        <item name="android:windowNoTitle">true</item>
        <item name="android:typeface">monospace</item>
        <item name="android:textColorPrimary">@android:color/white</item>
        <item name="android:itemTextAppearance">@style/MenuText</item>
   </style>

   <style name="M13Text">
        <item name="android:typeface">monospace</item>
        <item name="android:textColor">@android:color/white</item>
        <item name="android:textColorLink">@android:color/holo_red_light</item>
   </style>

   <style name="MenuText">
       <item name="android:typeface">monospace</item>
       <item name="android:textColor">@android:color/black</item>
   </style>
</resources>

All was fine until Lollipop arrived when it doesn't seem use the Monospace font anymore and I can see it change when I flip APIs from 19 to 21 in Android Studio.

I've googled and not found anything and I appreciate it is just a cosmetic issue but anyone got any ideas as to why?

Macrospore answered 16/3, 2015 at 12:16 Comment(4)
I have no problems using monospace on Android 5.0. This sample app works fine on a Nexus 4 -- I just tested it.Birt
Thanks @Birt . Curious. I'm obviously doing something wrong as setting the font directly doesn't work either... I wonder if it's the parent attribute overriding something?Macrospore
Have you tried setting android:fontFamily="@null"?Dingdong
Thanks @alanv. That's looking promising.Macrospore
D
32

The Material text appearances specify the android:fontFamily attribute rather than android:typeface so that they can use sans-serif-light, sans-serif-medium, etc. This attribute takes precedence over typeface, so you will need to either override or clear the fontFamily value.

<style name="MenuText">
    <item name="android:fontFamily">monospace</item>
    ...
</style>
Dingdong answered 17/3, 2015 at 20:42 Comment(3)
Thanks @alanv! That gave me the solution <item name="android:fontFamily">monospace</item> <item name="android:typeface">monospace</item> did the trick.Macrospore
Indeed the android:fontFamily should be set to "monospace" and not @null. For one-time setting of monospace you can also add android:fontFamily="monospace" as a property to your <TextView /> in your layout xml instead of defining a style for it. Not that defining styles is a bad thing, I just wanted to add that bit of info. Thanks for the answer and comment!Heckle
How do I subsequently set this style on a textview?Unnerve

© 2022 - 2024 — McMap. All rights reserved.