Calligraphy library by chrisjenx is not working
Asked Answered
B

4

19

I did what his documentation has instructed upon setting up the default font:

 @Override
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupDefaultFont();

        setContentView(R.layout.activity_main);

        setupToolbarAndNavigationDrawer();
  }

  public void setupDefaultFont() {
        CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                        .setDefaultFontPath("fonts/OpenSans-Regular.ttf")
                        .setFontAttrId(R.attr.fontPath)
                        .build()
        );
  }

I also placed the fonts in assets/fonts, but to no avail. Roboto still shows up as default font and not Open Sans. I tried applying it manually one by one to each TextView, but still it doesn't work.

Any ideas on why this doesn't work?

More info: (In case it is useful) My miniSdkVersion is 15 and targetSdkVersion is 22. These are my dependencies:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:design:22.2.0'
    compile 'com.android.support:recyclerview-v7:21.0.3'
    compile 'com.android.support:cardview-v7:21.0.3'
    compile 'de.hdodenhof:circleimageview:1.2.1'
    compile 'uk.co.chrisjenx:calligraphy:2.1.0'
}

And this is the custom theme that I am using.

<resources>
    <style name="myIIT_theme" parent="Theme.AppCompat">
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <item name="colorPrimary">@color/primary</item>
        <item name="android:textColorPrimary">@color/white</item>
        <item name="android:windowBackground">@color/tertiary_dark</item>
        <item name="android:activatedBackgroundIndicator">@drawable/selected_drawer</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
</resources>
Boland answered 14/7, 2015 at 4:53 Comment(0)
C
39

For the configuration to take effect, you should set up the default font in the onCreate() method of your custom application class, instead of in the activity.

Also, the instructions at https://github.com/chrisjenx/Calligraphy say to inject into the context, by overriding a method in the activity as follows:

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
Chadchadabe answered 14/7, 2015 at 10:17 Comment(4)
Thanks anyway! Just found out about it before you answered. Was supposed to answer it on my own here. Thanks though.Boland
So we need to override attachBaseContext() in every Activity?Cowell
Yes. Or create a common super class, and inherit every Activity from it.Chadchadabe
its not working anymore in appcompatViews in android API 26 !Cosper
B
4

Along with @Theo's answer, make sure you register your custom Application in the Manifest

<application
  android:name=".MyApplication" <----------- HERE
  android:allowBackup="true"
  android:icon="@mipmap/ic_launcher"
  android:label="@string/app_name"
  android:roundIcon="@mipmap/ic_launcher_round"
  android:supportsRtl="true"
  android:theme="@style/AppTheme">
Blayne answered 11/7, 2018 at 3:31 Comment(0)
S
1

as mentioned in the Readme file in github, This version of Calligraphy has reached its end-of-life and is no longer maintained. Please migrate to Calligraphy 3!

Stribling answered 1/6, 2019 at 9:47 Comment(0)
M
0

1.Please update your code with below the code:

 ViewPump.init(ViewPump.builder()
                .addInterceptor(new CalligraphyInterceptor(
                        new CalligraphyConfig.Builder()
                                .setDefaultFontPath("fonts/century_gothic.ttf")
                                .setFontAttrId(io.github.inflationx.calligraphy3.R.attr.fontPath)
                                .build()))
                .build());
  1. Put below the code in Class:

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(ViewPumpContextWrapper.wrap(newBase));
    }
    
Martymartyn answered 1/10, 2023 at 22:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.