Android – How to specify eg. Roboto-Medium or Roboto-Black in styles.xml

androidandroid-fontsandroid-typeface

Posts like this How to change fontFamily of TextView in Android suggests, that the variants of Roboto fonts you can specify in styles.xml in Android 4.2 boils down to the following:

  • Regular
  • Italic
  • Bold
  • Bold-italic
  • Light
  • Light-italic
  • Thin
  • Thin-italic
  • Condensed regular
  • Condensed italic
  • Condensed bold
  • Condensed bold-italic

That leaves out the ability to style TextViews using eg. the Roboto-Medium or Roboto-Black fonts.

But why would Google add system wide fonts that can not be used for styling of your TextViews? Surely there must be some way of specifying all of the Roboto fonts from within styles.xml (ie. NOT having to embed the fonts as Assets and creating custom TextViews in code) – but how?

Best Answer

On Android 5.0 you can set Roboto Medium with sans-serif-medium.

This solution, taken from Google iosched 2014, uses sans-serif on Android pre-v21:

values/styles.xml

<style name="MyStyle">
    <item name="android:fontFamily">@string/font_fontFamily_medium</item>
</style>

values/fonts.xml

<string name="font_fontFamily_medium">sans-serif</string>

values-v21/fonts.xml

<string name="font_fontFamily_medium">sans-serif-medium</string>