4

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.

Lau
  • 1,804
  • 1
  • 23
  • 49
  • You sure that your `@font/example` is exist ? –  Oct 24 '17 at 08:12
  • @Ibrahim Yes. It works if I set it programmatically: `setTypeface(ResourcesCompat.getFont(context, R.font.example));` – Lau Oct 24 '17 at 08:18
  • You cannot use custom font in style and xml. You need to create custom view or Set it programatically or use some available libraries like https://github.com/chrisjenx/Calligraphy – Arbaz Ali Rizvi Oct 24 '17 at 08:29
  • @ArbazRizvi according to this: https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html I can – Lau Oct 24 '17 at 08:34
  • @Lau strange, it's worked for me, you may check if you doing something with your textView xml. –  Oct 24 '17 at 08:37
  • @Lau Can you share your font-family xml – Arbaz Ali Rizvi Oct 24 '17 at 08:45
  • You can check this link for font family:https://stackoverflow.com/questions/19691530/valid-values-for-androidfontfamily-and-what-they-map-to – Sandeep Manmode Oct 24 '17 at 08:56

2 Answers2

3

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

Lau
  • 1,804
  • 1
  • 23
  • 49
1

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

Francis
  • 217
  • 2
  • 4