Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android custom fonts not working programmatically

I need to change my app's font to a custom font. I'm using Android Native Fonts for this. I have added my_font.ttf and my_font_italic.ttf on the font folder.

Also, I created this font family resource

<?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:font="@font/my_font"
        app:fontStyle="normal"
        app:font="@font/my_font"/>

    <font
        android:fontStyle="italic"
        android:font="@font/my_font_italic"
        app:fontStyle="italic"
        app:font="@font/my_font_italic"/>
</font-family>

On my layout, I have a LinearLayout with a couple of TextViews like this:

<LinearLayout
    android:id="@+id/test"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:textAppearance="@style/text_28_italic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/my_font"
        android:text="Test Text"/>

    <TextView
        android:textAppearance="@style/text_28_bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/my_font"
        android:text="Test Text"/>
</LinearLayout>

The styles I'm using are:

<style name="text_28_italic">
    <item name="android:fontFamily">@font/my_font</item>
    <item name="android:textSize">28sp</item>
    <item name="android:textStyle">italic</item>
</style>

<style name="text_28_bold">
    <item name="android:fontFamily">@font/my_font</item>
    <item name="android:textSize">28sp</item>
    <item name="android:textStyle">bold</item>
</style>

If I test the app, the fonts in the text views are shown correctly.

However, the problem comes when I try to add TextViews by code. According to the documentation, I need to use Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont); to set the font by code. But I also need to set a style so I'm using setTextAppearance() too.

Here is my code:

TextView tv1 = new TextView(this);
tv1.setText("Test text");
tv1.setTypeface(ResourcesCompat.getFont(this, R.font.my_font));
tv1.setTextAppearance(this, R.style.text_28_italic);
((LinearLayout) findViewById(R.id.test)).addView(tv1);

TextView tv2 = new TextView(this);
tv2.setText("Test text")
tv2.setTextAppearance(this, R.style.text_28_bold);
tv2.setTypeface(ResourcesCompat.getFont(this, R.font.my_font));
((LinearLayout) findViewById(R.id.test)).addView(tv2);

This way, I cannot make it work. I get the font applied, but not the text appearance.

How can I make both text appearance and typeface work programmatically?

Thanks!

like image 814
roy_lennon Avatar asked Sep 30 '26 06:09

roy_lennon


1 Answers

Typeface typeface = ResourcesCompat.getFont(this, R.font.roboto);

.............or........................

Typeface typeface = getResources.getFont(R.font.roboto);
textView.setTypeface(typeface);
like image 160
Javid Sattar Avatar answered Oct 01 '26 20:10

Javid Sattar