Nouman Ghaffar
Nouman Ghaffar

Reputation: 3830

Android Fonts in XML not working on 7.0

Hello everyone, I have used Fonts in xml for my app. It's working very well on android versions below 7.0. But not working on 7.0. I created a font folder inside drawable and put all my fonts in there. Then created a font family like this.

 <?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">
    <font
        android:font="@font/timeburnerbold"
        android:fontStyle="normal"
        android:fontWeight="400"
        app:font="@font/timeburnerbold"
        app:fontStyle="normal"
        app:fontWeight="400" />
    <font
        android:font="@font/typo_grotesk_bold"
        android:fontStyle="normal"
        android:fontWeight="700"
        app:font="@font/typo_grotesk_bold"
        app:fontStyle="normal"
        app:fontWeight="700" />
</font-family>

My Gradle file info

compileSdkVersion 27
buildToolsVersion "26.7.2"
supportLibraryVersion = '27.0.0'

And I m using this across the app. It's working perfectly across the app on 6.0 and below. But not on 7.0. Don't know what I m missing here. Anyone?

Upvotes: 0

Views: 2293

Answers (1)

Nouman Ghaffar
Nouman Ghaffar

Reputation: 3830

It was very strange but I found the solution. Seems like a bug in SDK. In a font -family, If you have two fonts of different families like below xml ( My case )

    <?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">
    <font
        android:font="@font/timeburnerbold"
        android:fontStyle="normal"
        android:fontWeight="400"
        app:font="@font/timeburnerbold"
        app:fontStyle="normal"
        app:fontWeight="400" />
    <font
        android:font="@font/typo_grotesk_bold"
        android:fontStyle="normal"
        android:fontWeight="700"
        app:font="@font/typo_grotesk_bold"
        app:fontStyle="normal"
        app:fontWeight="700" />
</font-family>

This type of file will not work on 7.0 to onwards. Changing it to

<?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">
    <font
        android:font="@font/typo_grotesk_regular"
        android:fontStyle="normal"
        android:fontWeight="400"
        app:font="@font/typo_grotesk_regular"
        app:fontStyle="normal"
        app:fontWeight="400" />
    <font
        android:font="@font/typo_grotesk_regular"
        android:fontStyle="italic"
        android:fontWeight="400"
        app:font="@font/typo_grotesk_regular"
        app:fontStyle="italic"
        app:fontWeight="400" />
    <font
        android:font="@font/typo_grotesk_bold"
        android:fontStyle="normal"
        android:fontWeight="700"
        app:font="@font/typo_grotesk_bold"
        app:fontStyle="normal"
        app:fontWeight="700" />
    <font
        android:font="@font/typo_grotesk_bold"
        android:fontStyle="italic"
        android:fontWeight="700"
        app:font="@font/typo_grotesk_bold"
        app:fontStyle="italic"
        app:fontWeight="700" />
</font-family>

Really works on all api's. Same font family in one font family file. Don't know why it really happens.

Upvotes: 1

Related Questions