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?

Grumpy Old Jon
  • 113
  • 1
  • 1
  • 8
  • I have no problems using `monospace` on Android 5.0. [This sample app](https://github.com/commonsguy/cw-omnibus/tree/master/Fonts/FontSampler) works fine on a Nexus 4 -- I just tested it. – CommonsWare Mar 16 '15 at 12:24
  • Thanks @CommonsWare . 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? – Grumpy Old Jon Mar 16 '15 at 13:00
  • Have you tried setting `android:fontFamily="@null"`? – alanv Mar 16 '15 at 17:20
  • Thanks @alanv. That's looking promising. – Grumpy Old Jon Mar 17 '15 at 12:26

1 Answers1

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>
alanv
  • 23,966
  • 4
  • 93
  • 80
  • 3
    Thanks @alanv! That gave me the solution `monospace monospace` did the trick. – Grumpy Old Jon Mar 19 '15 at 09:22
  • 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 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! – baske Mar 24 '15 at 14:09
  • How do I subsequently set this style on a textview? – Scorb Nov 03 '16 at 16:35