I cannot set a custom font in a 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 the default font in my application. But when I change fontFamily
to one of the defaults, it changes
<style name="fontTextView" parent="@android:style/Widget.TextView">
<item name="android:fontFamily">monospace</item>
</style>
It also works if I install the software font programmatically.
setTypeface(ResourcesCompat.getFont(context, R.font.example));
How to make it work for my custom font in styles.xml
?
I installed compiltSdkVersion
in 26
and buildToolsVersion
in 26.0.2
. I also use the 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 file *ttf
for testing purposes.
source
share