Lau
Lau

Reputation: 1834

Android Studio 3.0 Custom font in styles.xml

I'm unable to set the custom font in my style file.

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        ...
        <item name="android:textViewStyle">@style/fontTextView</item>
        ...
    </style>

    <style name="fontTextView" parent="@android:style/Widget.TextView">
        <item name="android:fontFamily">@font/example</item>
    </style>
</resources>

and I still see default font in my app. But when I change fontFamily to one of the default ones, then it changes

    <style name="fontTextView" parent="@android:style/Widget.TextView">
        <item name="android:fontFamily">monospace</item>
    </style>

It also works if I set my custom font programmatically.

setTypeface(ResourcesCompat.getFont(context, R.font.example));

How to make it works for my custom font in styles.xml?

I set compiltSdkVersion to 26 and buildToolsVersion to 26.0.2. Also I'm using support library:

  def supportVersion = '26.1.0'
  implementation "com.android.support:appcompat-v7:$supportVersion"

My font family:

<?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:fontStyle="normal"
    android:fontWeight="400"
    android:font="@font/example_regular"

    app:fontStyle="normal"
    app:fontWeight="300"
    app:font="@font/example_regular"/>

<font
    android:fontStyle="normal"
    android:fontWeight="400"
    android:font="@font/example_regular"

    app:fontStyle="normal"
    app:fontWeight="400"
    app:font="@font/example_regular"/>

<font
    android:fontStyle="normal"
    android:fontWeight="400"
    android:font="@font/example_regular"

    app:fontStyle="normal"
    app:fontWeight="700"
    app:font="@font/example_regular"/>

<font
    android:fontStyle="italic"
    android:fontWeight="400"
    android:font="@font/example_regular"

    app:fontStyle="italic"
    app:fontWeight="300"
    app:font="@font/example_regular" />

<font
    android:fontStyle="italic"
    android:fontWeight="400"
    android:font="@font/example_regular"

    app:fontStyle="italic"
    app:fontWeight="400"
    app:font="@font/example_regular" />

<font
    android:fontStyle="italic"
    android:fontWeight="400"
    android:font="@font/example_regular"

    app:fontStyle="italic"
    app:fontWeight="700"
    app:font="@font/example_regular" />

</font-family>

I Use the same *ttf file for test purpose.

Upvotes: 4

Views: 5081

Answers (2)

Francis
Francis

Reputation: 247

using custom font for xml design : First you need to create font folder in your resource directory after that put all custom font(.ttf file) file in to font folder. then just add attribute fontFamily in xml design . Like

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:textSize="40dp"
    android:fontFamily="@font/semibold"/>

use that link https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html

Upvotes: 1

Lau
Lau

Reputation: 1834

Ok. I solve this issue - the reason of this behavior is very simple... I was extending Activity instead of AppCompatActivity

Upvotes: 3

Related Questions