0

How to maintain a custom font for the entire app. Instead of loading the file from assets in Android. For example like below

TextView tv =(TextView)findViewById(R.id.textview1);
Typeface type =Typeface.createfromAssets(this,"custom.ttf");
tv.setTypeface(type);
Raja Jawahar
  • 6,742
  • 9
  • 45
  • 56
  • http://stackoverflow.com/questions/12281761/android-how-to-set-a-custom-font-for-whole-app – IntelliJ Amiya Jan 13 '16 at 15:09
  • Google search not working in your location ? I found several dupes using the following search terms : *custom font application android*. I wonder why you didn't do the same... Oh well. – 2Dee Jan 13 '16 at 15:11
  • Please make sure you instantiate the `Typeface` only once... creating the `Typeface` takes time. – EpicPandaForce Jan 13 '16 at 15:17

2 Answers2

1

Create a new Class like 'CustomTextView' which extends TextView.

By default, set the typeface to this CustomTextView and use your CustomTextView in the rest of your app.

Nicolas Cortell
  • 659
  • 4
  • 16
1

You can define a custom font for your application with Calligraphy (https://github.com/chrisjenx/Calligraphy)

Add the dependency in your build.gradle :

dependencies {
    ...
    compile 'uk.co.chrisjenx:calligraphy:2.1.0'
    ...
}

Add Fonts

Add your custom fonts to a assets/fonts/ folder

Installation, setup default font for your entire app

Define your default font using CalligraphyConfig, in your Application class in the #onCreate() method.

@Override
public void onCreate() {
    super.onCreate();
    CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                            .setDefaultFontPath("fonts/Roboto-RobotoRegular.ttf")
                            .setFontAttrId(R.attr.fontPath)
                            .build()
            );
    //....
}

Note: You don't need to define CalligraphyConfig but the library will apply no default font and use the default attribute of R.id.fontPath.

In every Activity :

Do this in every Activity of your App where you want the new font:

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

You're good to go!

victorleduc
  • 232
  • 2
  • 9
  • I did the above steps. Default font is replaced successfully, but bold, italic, etc styles are not applied to the new font on device. – code_poetry Oct 21 '16 at 07:21
  • it is working on my fragments and activity but not on textview that I'm creating programmatically for a gridview on a custom dialog – mohas Dec 12 '18 at 19:32