Applying custom font to the whole Android Application?

614 views Asked by At

Before you jump with negative comments/votes, I will specify that I have read every question on `Stackoverflow regarding this topic, and have applied almost all the solutions, but still I have not succeeded applying it entirely.

So, I am asking this question, because almost all the answers I found are for like 4-5 years ago, and I was wondering if there are better solutions now.

I repeat my question: Is there a better way than those listed to override the whole font family in the whole app to only 1 font? (All the views used in the app)?

Thanks in advance!

Cheers!

3

There are 3 answers

0
Yash Sampat On BEST ANSWER

The best way is still to put your .ttf or .otf font file in the assets folder. Then derive a custom TextView class and fix its' font once and for all so that you don't have to call setTypeface() everywhere.

That is all.

0
Hiren Patel On

Custom TextView Java Class

Put this class in com.util package

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class CustomTextViewRegular extends TextView {

    public CustomTextViewRegular(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

    public CustomTextViewRegular(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);

    }

    public CustomTextViewRegular(Context context) {
        super(context);
        init(null);
    }

    private void init(AttributeSet attrs) {
        if (attrs != null) {
//          Set type face
            Typeface myTypeface = Typeface.createFromAsset(getContext()
                    .getAssets(), "Helvetica_Neue.ttf");
            setTypeface(myTypeface);
        }
    }

}

You must have Helvetica_Neue.ttf in assets folder, you can use other font as well.

How to use in xml

<com.util.CustomTextViewRegular
    android:id="@+id/txtCustom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"
    android:textColor="#727272"
    android:textSize="20dip" />

If you want to change font than you have to just change font type in Java class only.

Done

0
Vlad Iancu On

I have found no work-arounds for setting one font on the app, so I resorted in calling setTypeface() for each view in each of the fragment/activities. Thanks, and I apologize for the long time in which i did not answer! I well deserve the downvotes, hehe.

Cheers everyone! I will pay more attention to asking questions and my behavior towards the users answering them in the future.