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.

sziraqui
  • 5,763
  • 3
  • 28
  • 37

6 Answers6

66

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"/>
Jemshit
  • 9,501
  • 5
  • 69
  • 106
Yuri Misyac
  • 4,364
  • 2
  • 16
  • 32
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.

ra3o.ra3
  • 852
  • 1
  • 8
  • 7
  • 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 '17 at 09:37
  • did this work? some people are saying you must upgrade to Android Studio 3 for this to work. – user1506104 Dec 20 '17 at 12:50
  • 3
    Thanks for the pointer. I changed my to and it works now. – tmm1 Jan 09 '18 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

JaydeepW
  • 3,237
  • 1
  • 25
  • 27
Jemshit
  • 9,501
  • 5
  • 69
  • 106
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+

sziraqui
  • 5,763
  • 3
  • 28
  • 37
2

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

https://github.com/mukundrd/androidFonts

Mukund Desai
  • 100
  • 2
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());
RegularGuy
  • 3,516
  • 1
  • 12
  • 29