3

I am trying to swap out calligraphy (very useful library) for android fonts in xml but i dont know how to change the default font for all views like with calligraphy. Calligraphy was able to change the default font at initialization time. How can i do this with android fonts support library ?

from the fonts in xml article on android developer site i see we can apply a font family to one view like this:

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

but what if i want to change the font of the entire app ?

the reason i'd like to use android xml fonts is due to this error but this is not really relevant to the question.

j2emanue
  • 60,549
  • 65
  • 286
  • 456
  • Hi, I have the same question. I've been working to try get the same behaviour. This is the work so far, I got default for Toolbar, Action Bar, TextView, Button, Tab Layout, EditText, I cannot seem get default for Checkbox and Radio Button. Most of the logic is in the styles.xml. Let's work together to figure this out Here is the repro to give it a try https://github.com/erseno/Android-Supp-26-Default-Font – Ersen Osman Oct 17 '17 at 19:46

1 Answers1

0

With the Support Library 26.1.0 I managed to have a default font for the whole app by setting the android:fontFamily attribute for the application theme:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
     ...
    <item name="android:fontFamily">@font/proxima_nova</item>
</style>

Where proxima_nova.xml is a font resource file:

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="UnusedAttribute">
    <!-- As of Android Support Library 26.0, we must declare both sets of attributes to ensure
         our fonts load on devices running Android 8.0 (API level 26) or lower. -->
    <font
        android:font="@font/proxima_nova_light"
        android:fontStyle="normal"
        android:fontWeight="300"
        app:font="@font/proxima_nova_light"
        app:fontStyle="normal"
        app:fontWeight="300" />
    <font
        android:font="@font/proxima_nova_semibold"
        android:fontWeight="600"
        app:font="@font/proxima_nova_semibold"
        app:fontWeight="600" />
</font-family>
makovkastar
  • 5,000
  • 2
  • 30
  • 50
  • 1
    after setting this default font were you able to override the font for a single view. so imagine the default font is proxima_nova, but for a textview you wanted it to helvitica, are you you able to override it ? – j2emanue Oct 27 '17 at 02:40
  • Yes, of course. You can still set a different font family for a TextView. For example: android:fontFamily="@font/helvitica" – makovkastar Oct 27 '17 at 07:21
  • 2
    no, you cant. android:fontFamily="@font/helvitica" for text view will not work – Tooto Nov 23 '18 at 14:58