71

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?

Community
  • 1
  • 1
ckibsen
  • 943
  • 1
  • 7
  • 9

4 Answers4

99

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>
rciovati
  • 27,603
  • 6
  • 82
  • 101
46

NEW SOLUTION

After Google I/O '17 you can create multiple font family in res/font folder

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
<font
    app:font="@font/roboto_light"
    app:fontStyle="normal"
    app:fontWeight="300" />
<font
    app:font="@font/roboto_bold"
    app:fontStyle="normal"
    app:fontWeight="700" />

</font-family>

and you can use wherever want

 <style name="AppTheme.DefaultTextView" parent="android:style/Widget.TextView">
    <item name="android:textColor">@color/colorBlack</item>
    <item name="android:fontFamily">@font/font_family_roboto</item>
</style>

font folder font family

OLD ANSWER

Thanks for Jakob Eriksson

original answer

android:fontFamily="sans-serif"           // roboto regular
android:fontFamily="sans-serif-light"     // roboto light
android:fontFamily="sans-serif-condensed" // roboto condensed
android:fontFamily="sans-serif-black"     // roboto black
android:fontFamily="sans-serif-thin"      // roboto thin (android 4.2)
android:fontFamily="sans-serif-medium"    // roboto medium (android 5.0)

in style:

<style name="AppTheme.DefaultTextView" parent="android:style/Widget.TextView">
    <item name="android:textSize">@dimen/text_view_text_size</item>
    <item name="android:textColor">@color/black</item>
    <item name="android:fontFamily">sans-serif-light</item>
    <item name="android:textStyle">normal</item>
</style>

in app theme:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/gray</item>
    <item name="android:textViewStyle">@style/AppTheme.DefaultTextView</item>
</style>
Abror Esonaliev
  • 11,797
  • 2
  • 23
  • 20
  • I get an error: Element "font-family" must be declared – Someone Somewhere Apr 24 '19 at 18:45
  • and how to use them for specific elements? – user924 Sep 22 '22 at 15:46
  • how do you select 300, 400, 500, 600 for textview, `android:fontFamily`, is not enough, you have to specify which font you want to use, thing, light, medium, bold and many others. Android only has `normal/italic/bold` defaults for `textStyle` attribute – user924 Sep 22 '22 at 15:49
5

Support Library 26 introduced using fonts in XML, and it's backwards compatible to Android API 14.

To add fonts as resources, perform the following steps in the Android Studio:

  1. Right-click the res folder and go to New > Android resource directory. The New Resource Directory window appears.

  2. In the Resource type list, select font, and then click OK. Note: The name of the resource directory must be font.

Figure 1. Adding the font resource directory

  1. Add your font files in the font folder. The folder structure below generates R.font.dancing_script, R.font.lobster, and R.font.typo_graphica.

Figure 2. Adding the font files in the resource directory

  1. Double-click a font file to preview the file's fonts in the editor.

Figure 3. Previewing the font file

To set a font for the TextView, in the layout XML file, set the fontFamily attribute to the font file you want to access.

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/lobster"/>

The Android Studio layout preview allows you to preview the font set in the TextView.

Figure 4. Previewing fonts in layout preview

To add a font to a style, Open the styles.xml, and set the fontFamily attribute to the font file you want to access.

<style name="customfontstyle" parent="@android:style/TextAppearance.Small">
<item name="android:fontFamily">@font/lobster</item>

If you don't want to bundle the fonts in your app, you can always look into Downloadable Fonts

Andrew Orobator
  • 7,978
  • 3
  • 36
  • 36
  • 1
    This is helpful, but the OP asked about using system fonts like Roboto-Medium etc. Surely you don't have to mess with bundling the TTF files into your app, or downloading them, when the font already comes with the system? – LarsH Feb 24 '21 at 21:16
  • and here I am also struggling with this 7 years after the initial post... – jteichert Jun 14 '21 at 13:22
4

I suggest you to use custom libraries like Android-RobotoTextView or Calligraphy. With one of these you can set the font of a view in xml with an attribute so you can put in in styles.xml . And they works with previous version of Android than 4.0

Rampo
  • 249
  • 1
  • 2
  • 12