Valid values for android:fontFamily and what they map to?
Asked Answered
U

3

291

In the answer to this question the user lists values for android:fontFamily and 12 variants (see below). Where do these values come from? The documentation for android:fontFamily does not list this information in any place (I checked here, and here). The strings are listed in the Android styles.xml file in various places, but how do these map back to the Roboto font?

From android 4.1 / 4.2, the following Roboto font families are available:

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

in combination with this

android:textStyle="normal|bold|italic" 

12 variants are possible:

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

In the styles.xml file in the application I'm working on somebody listed this as the font family, and I'm pretty sure it's wrong:

<item name="android:fontFamily">Roboto-Regular.ttf</item>

I'd like to get the theme for our app set up correctly (which includes using fontFamily correctly) and remove all the redundancy that is in some of the styles that were created before I had a look at the file.

Upbraiding answered 30/10, 2013 at 18:57 Comment(0)
D
369

Where do these values come from? The documentation for android:fontFamily does not list this information in any place

These are indeed not listed in the documentation. But they are mentioned here under the section 'Font families'. The document lists every new public API for Android Jelly Bean 4.1.

In the styles.xml file in the application I'm working on somebody listed this as the font family, and I'm pretty sure it's wrong:

Yes, that's wrong. You don't reference the font file, you have to use the font name mentioned in the linked document above. In this case it should have been this:

<item name="android:fontFamily">sans-serif</item>

Like the linked answer already stated, 12 variants are possible:

Added in Android Jelly Bean (4.1) - API 16 :

Regular (default):

<item name="android:fontFamily">sans-serif</item>
<item name="android:textStyle">normal</item> 

Italic:

<item name="android:fontFamily">sans-serif</item>
<item name="android:textStyle">italic</item>

Bold:

<item name="android:fontFamily">sans-serif</item>
<item name="android:textStyle">bold</item>

Bold-italic:

<item name="android:fontFamily">sans-serif</item>
<item name="android:textStyle">bold|italic</item>

Light:

<item name="android:fontFamily">sans-serif-light</item>
<item name="android:textStyle">normal</item>

Light-italic:

<item name="android:fontFamily">sans-serif-light</item>
<item name="android:textStyle">italic</item>

Thin :

<item name="android:fontFamily">sans-serif-thin</item>
<item name="android:textStyle">normal</item>

Thin-italic :

<item name="android:fontFamily">sans-serif-thin</item>
<item name="android:textStyle">italic</item>

Condensed regular:

<item name="android:fontFamily">sans-serif-condensed</item>
<item name="android:textStyle">normal</item>

Condensed italic:

<item name="android:fontFamily">sans-serif-condensed</item>
<item name="android:textStyle">italic</item>

Condensed bold:

<item name="android:fontFamily">sans-serif-condensed</item>
<item name="android:textStyle">bold</item>

Condensed bold-italic:

<item name="android:fontFamily">sans-serif-condensed</item>
<item name="android:textStyle">bold|italic</item>

Added in Android Lollipop (v5.0) - API 21 :

Medium:

<item name="android:fontFamily">sans-serif-medium</item>
<item name="android:textStyle">normal</item>

Medium-italic:

<item name="android:fontFamily">sans-serif-medium</item>
<item name="android:textStyle">italic</item>

Black:

<item name="android:fontFamily">sans-serif-black</item>
<item name="android:textStyle">italic</item>

For quick reference, this is how they all look like:

Dipstick answered 30/10, 2013 at 19:29 Comment(23)
Many thanks @Dipstick How did you find that link? Is your Google-Fu better than mine?Upbraiding
I traced the creation of the actual font in TextView all the way down to the point of no return, and found no mapping. There are calls into native code.Upbraiding
Haha :D I actually knew about the site before that, so finding it wasn't difficult. But for future reference: I prefer to use the search on developer.android.com over the google search, since you can limit the search to only the API's, blog posts, release notes etc.Dipstick
And if you are interested: This is where the fonts are defined. This class loads the fonts and this one manages them afaict.Dipstick
By the way, older versions of Calligraphy use the fontFamily tag to define custom fonts via their filename directly which could be what the OP saw in their code.Jobless
What's the default one that is used? Also, what's "Roboto-medium" as shown here: commondatastorage.googleapis.com/androiddevelopers/design/… ?Chaumont
@androiddeveloper Ah yes, thanks for that! Reboto Medium and Roboto Black have been added in Roboto V2 that came out with Android Lollipop, as it can be seen in the diff. As to what the default font is: it's Roboto Regular.Dipstick
@Dipstick What's Roboto-Black? How come a color is a part of the name? Odd. Anyway, can you please add a tiny info for each of the fonts, as to on which API it got added? Also maybe write that the default one is "Roboto-Regular" .Chaumont
@androiddeveloper done. Roboto black does not refer to the color of the font, but is just a "more bolder" version of of Roboto Bold. I've added a reference image at the bottom.Dipstick
@Dipstick Thank you for clarifying this. Here's your +1 ... :)Chaumont
@Dipstick Why does "Black" also have "italic" (meaning you named it "Black Italic") ? it doesn't seem italic to me... Also, why do you have "Condensed regular"? it's named just "Condensed", no?Chaumont
also, there is a lot of alias to use with fontFamily android.googlesource.com/platform/frameworks/base/+/master/data/…Bradfordbradlee
what about roboto-condensed-light ? Can't find since which platform it's available. Can't Google document that properly somewhere ??Evanston
way to use these in API : 14 ? android:fontFamily="sans-serif-thin" android:textStyle="normal"Hygro
@abdulwahab Include them in your assets and apply the dont to your textviews.Dipstick
Im using sans-serif-light on my app. My photo me is KitKat and m friends is lollipop. When I tested the app on theirs the font was different. Thicker then the sans-serif-light! How can I make it light on all phones?Madrid
@Smiffy Some manufacturers use custom fonts instead of Roboto, iirc Samsung for example. You would have to include the Roboto in your assets folder and set the font to your TextView, if you really want it to be the same on every device.Dipstick
@Dipstick sigh manufactures making our job harder :(. But I'm really happy now as I know what to do :)!! Thanks Ahmad!Madrid
@Smiffy Oh yeah! especially Samsung :/ No problem!Dipstick
@Dipstick haha that's funny as that is the device I'm having the problem with xD. Thanks again :)Madrid
Are there any new available fonts to use on Nougat ?Chaumont
<item name="android:fontFamily">sans-serif-light</item> <item name="android:textStyle">italic|bold</item> also works even though I have not seen it mentioned anywhereTranquil
Replace "TextView" with "android.support.v7.widget.AppCompatTextView". Replace "android:fontFamily" with "app:fontFamily". It will work below 16.Geophagy
U
140

Available fonts (as of Oreo)

Preview of all fonts

The Material Design Typography page has demos for some of these fonts and suggestions on choosing fonts and styles.

For code sleuths: fonts.xml is the definitive and ever-expanding list of Android fonts.


Using these fonts

Set the android:fontFamily and android:textStyle attributes, e.g.

<!-- Roboto Bold -->
<TextView
    android:fontFamily="sans-serif"
    android:textStyle="bold" />

to the desired values from this table:

Font                     | android:fontFamily          | android:textStyle
-------------------------|-----------------------------|-------------------
Roboto Thin              | sans-serif-thin             |
Roboto Light             | sans-serif-light            |
Roboto Regular           | sans-serif                  |
Roboto Bold              | sans-serif                  | bold
Roboto Medium            | sans-serif-medium           |
Roboto Black             | sans-serif-black            |
Roboto Condensed Light   | sans-serif-condensed-light  |
Roboto Condensed Regular | sans-serif-condensed        |
Roboto Condensed Medium  | sans-serif-condensed-medium |
Roboto Condensed Bold    | sans-serif-condensed        | bold
Noto Serif               | serif                       |
Noto Serif Bold          | serif                       | bold
Droid Sans Mono          | monospace                   |
Cutive Mono              | serif-monospace             |
Coming Soon              | casual                      |
Dancing Script           | cursive                     |
Dancing Script Bold      | cursive                     | bold
Carrois Gothic SC        | sans-serif-smallcaps        |

(Noto Sans is a fallback font; you can't specify it directly)

Note: this table is derived from fonts.xml. Each font's family name and style is listed in fonts.xml, e.g.

<family name="serif-monospace">
    <font weight="400" style="normal">CutiveMono.ttf</font>
</family>

serif-monospace is thus the font family, and normal is the style.


Compatibility

Based on the log of fonts.xml and the former system_fonts.xml, you can see when each font was added:

  • Ice Cream Sandwich: Roboto regular, bold, italic, and bold italic
  • Jelly Bean: Roboto light, light italic, condensed, condensed bold, condensed italic, and condensed bold italic
  • Jelly Bean MR1: Roboto thin and thin italic
  • Lollipop:
    • Roboto medium, medium italic, black, and black italic
    • Noto Serif regular, bold, italic, bold italic
    • Cutive Mono
    • Coming Soon
    • Dancing Script
    • Carrois Gothic SC
    • Noto Sans
  • Oreo MR1: Roboto condensed medium
Unscientific answered 6/6, 2014 at 0:44 Comment(3)
how did you find on the log about the Android version ?Chaumont
@androiddeveloper I was looking at the dates when each line was added. To know precisely which fonts are available in a particular version, look at system_fonts.xml for that version, e.g. for Lollipop: android.googlesource.com/platform/frameworks/base/+/…Unscientific
My one and only gripe about this is it is not alphabetized :p But still! This should be the best answer! Thank you!Alysonalysoun
O
9

As far as I'm aware, you can't declare custom fonts in xml or themes. I usually just make custom classes extending textview that set their own font on instantiation and use those in my layout xml files.

ie:

public class Museo500TextView extends TextView {
    public Museo500TextView(Context context, AttributeSet attrs) {
        super(context, attrs);      
        this.setTypeface(Typeface.createFromAsset(context.getAssets(), "path/to/font.ttf"));
    }
}

and

<my.package.views.Museo900TextView
        android:id="@+id/dialog_error_text_header"
        android:layout_width="190dp"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textSize="12sp" />
Oscilloscope answered 30/10, 2013 at 19:29 Comment(1)
This can cost you a lot of memory in a recyclerviewUthrop

© 2022 - 2024 — McMap. All rights reserved.