46

https://developer.android.com/preview/features/working-with-fonts.html

Android API 26 supports this but do we have a support library that can provide use of fonts as a resource in res? [ font in XML ] If yes, up-to what API is it supported?

I am talking about the Android O SDK feature which allows us to set font in XML as given in the link above. This is not a native feature in MM I m sure.

9
  • I'm using this feature in one of my apps, which supports API 9 to API 18. There's no limitation as far as I know. I stopped supporting API 8, but I guess it would have worked with it as well. Jun 12, 2017 at 18:13
  • Which library you have added to gradle?
    – sziraqui
    Jun 12, 2017 at 18:15
  • NONE. It's a native feature. By the way, Eclipse doesn't need all that "Gradle thing" ;) Jun 12, 2017 at 18:15
  • 1
    I think I m being misunderstood. I m talking about "font in XML" feature that was introduced in O. Edited question to clarify
    – sziraqui
    Jun 12, 2017 at 18:20
  • 1
    with some little trick != a native feature
    – sziraqui
    Jul 22, 2017 at 20:21

6 Answers 6

64

To support font family on API lower than 26, we need to use

<font app:fontStyle="normal" app:fontWeight="400" app:font="@font/myfont-Regular"/>

instead of

<font android:fontStyle="normal" android:fontWeight="400" android:font="@font/myfont-Regular"/>
2
37

According to Google it is supported on Android 4.0+ (API 14+) as long as these conditions are met:

  • You are using the support library version 26.0.0-beta1 or later
  • You are using AppCompat

Sources:

Fonts in XML - Using the support library

Support Library revision history

I was hoping I could use this in app widgets on Android versions earlier than 8 (API 26), however it's not possible, as AppCompatTextView cannot be used in app widgets. None of the third party alternatives to Android O's 'Fonts in XML' such as the Caligraphy library work in app widgets either.

The only alternative for app widgets is using an ImageView instead of a TextView and using RemoteViews methods such as setImageViewUri(...) and setImageViewBitmap(...), both of which are problematic.

3
  • Thanks. Why were others saying that its a native feature? I m using SDK Android Studio 2.3.2. I tried using @font/arial.ttf. It didnt work. I was using support library version 25.3.1. Now I know what was wrong.
    – sziraqui
    Jul 21, 2017 at 9:37
  • did this work? some people are saying you must upgrade to Android Studio 3 for this to work. Dec 20, 2017 at 12:50
  • 3
    Thanks for the pointer. I changed my <TextView> to <android.support.v7.widget.AppCompatTextView> and it works now.
    – tmm1
    Jan 9, 2018 at 22:17
29

To complete Yuri's answer, Fonts are supported back to API 16. To create font family (where different weights of fonts are specified), you should create my_font_family.xml under /res/font/ as written in the doc:

<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
    <font app:fontStyle="normal" app:fontWeight="400" app:font="@font/myfont_regular"/>
    <font app:fontStyle="normal" app:fontWeight="600" app:font="@font/myfont_semi_bold" />
    <font app:fontStyle="italic" app:fontWeight="400" app:font="@font/myfont_italic" />
</font-family>

To use this in TextView xml, you should use app:fontFamily="@font/my_font_family", be careful with app: instead of android:.

To choose specific fontWeight, you can use android:textFontWeight="200" but it only works on API>=28. To choose different font based on weight prior to API 28, you have two options. You can either use android:textStyle="bold" to specify only "normal/italic/bold", but not exact weight. Or you can directly use specific weight of font like this: app:fontFamily="@font/myfont_semi_bold".

Please Note: If you are using custom textview, YourTextView, in your app, it has to extend AppCompatTextView and not merely android.widget.TextView

4
  • Did you try textFontWeight? Because even on API 28 it doesn't work for me. Apr 14, 2019 at 18:02
  • 1
    It doesn't work for me, I am running in an emulator . I am using implementation 'androidx.appcompat:appcompat:1.0.2' and MaterialComponents theme Aug 9, 2019 at 14:07
  • @GaneshChowdharySadanala Please check the note added, that might help you.
    – JaydeepW
    Mar 29, 2020 at 6:30
  • textStyle solution doesn't work on Android 5 with support library
    – AAverin
    Apr 12, 2021 at 14:34
7

Some users will land up to this page searching for instructions to use custom fonts in your app from xml. Here's how I did it:

The font resource directory is not present by default. You can create it manually by following the documentation here [too much work]

Or use font selector from GUI-

  • Select a TextView in Design tab.

  • Open drop down of fontFamily xml attribute.

  • Scroll down to "More Fonts".

  • Choose a 'Downloadable font" from the pop up that appears.

Android Studio will automatically create font resource directory with necessary declarations mentioned in documentation above. After this step you can copy your custom font file (say myfont.ttf) in font directory and set your desired font from xml for example:

<TextView
     android:fontFamily="@font/myfont"
     android:id="@+id/aboutus_head_textview"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="@string/who_are_we" />

If you want to use the same font throughout your app, you can set fontFamily your AppTheme in styles.xml:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <!-- custom font for the entire app -->
        <item name="fontFamily">@font/myfont</item>
    </style>

[ prefix fontFamily with android: if above did not work ]

Screenshots:

Screenshot 1

Screenshot 2

Note: This answer assumes you are using Android Studio 3+ and support library version 26+

2

Here is an example, min sdk support 18 (in app)

https://github.com/mukundrd/androidFonts

1
  • Is it posible to include older APIs?
    – rraallvv
    Jun 24, 2019 at 15:44
0

This might be an undesired way of doing it, but if you have a global font (typeface) in xml, you could just create a Textview and get the typeface from there

TextView dummy = new TextView(context);
doSomethingWithTheTypeface(dummy.getTypeface());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.