Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change default font of the android app?

I want to make an app in a non English language. So I need the textviews, strings and the toast to be in the non-English language. Setting the typeface for every text is cumbersome. Is there a way to set a default font? When I have to refer to something in English (like email id) then I can use the textview.settypeface(xyz) method.

like image 682
suku Avatar asked Jan 03 '16 02:01

suku


People also ask

How do you change font style on apps?

Change Your Font Style in Android Settings As an example, on Samsung Galaxy devices the default pathway is Settings > Display > Font and screen zoom > Font Style. Afterward, you can tap to select a font, see the immediate change, and select Apply to confirm your new selection.

What is Android app default font?

"Roboto and Noto are the standard typefaces on Android and Chrome." From Wiki, "Roboto is a sans-serif typeface family developed by Google as the system font for its mobile operating system Android."

How do I change my default font?

Go to Format > Font > Font. + D to open the Font dialog box. Select the font and size you want to use. Select Default, and then select Yes.


2 Answers

There is a grate library for custom fonts in android: custom fonts

Here is a sample how to use it.

In gradle you need to put this line:

compile 'uk.co.chrisjenx:calligraphy:2.1.0'

Then make a class that extends application and write this code:

public class App extends Application { 
@Override public void onCreate() {
super.onCreate();

CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                .setDefaultFontPath("your font path")
                .setFontAttrId(R.attr.fontPath)
                .build()
);
}
}

In the activity class put this method before onCreate:

@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

In your manifest file write like this:

<application
android:name=".App"

It will change the whole activity to your font!. I'ts simple solution and clean!

like image 164
Shia G Avatar answered Sep 19 '22 20:09

Shia G


You can achieve this in mainly 3 ways. one way would be to create a custom TextView and refer that everywhere, ie :

    public class TypefacedTextView extends TextView {

    public TypefacedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        Typeface typeface = Typeface.createFromAsset(context.getAssets(), fontName);
        setTypeface(typeface);
    }
}

and inside View.xml

<packagename.TypefacedTextView
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="Hello"/>

Another way, is to use the powerful Calligraphy library in github. see this

And finally, you can override the defaults with your own fonts, see this

like image 31
OBX Avatar answered Sep 21 '22 20:09

OBX