I want to use chrisjenx/Calligraphy in Theme to change the font in my Whole project.
I followed the instructions but it's not working for me
here's my code:
in application :
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(base));
}
@Override
public void onCreate() {
super.onCreate();
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("BElham.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
}
here's my style
<!-- Base application theme. -->
<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">>
<item name="android:textViewStyle">@style/AppTheme.Widget.TextView</item>
</style>
<style name="AppTheme" parent="AppBaseTheme">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.Widget"/>
<style name="AppTheme.Widget.TextView" parent="android:Widget.TextView">
<item name="fontPath">BElham.ttf</item>
</style>
You have to write following code to each activity and not in Application class. Better put this in BaseActivity and extend it in all other Activities
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(base));
}
I am able to use your code and implement at my end.
try this first create an application class
MyApplication.java
@Override
public void onCreate() {
TypefaceUtil.overrideFont(getApplicationContext(), "SERIF", "fonts/Roboto-Regular.ttf"); // font from assets: "assets/fonts/chrisjenx.ttf
}
Then create a class TypefaceUtil.java
import android.content.Context;
import android.graphics.Typeface;
import java.lang.reflect.Field;
public class TypefaceUtil {
public static void overrideFont(Context context,
String defaultFontNameToOverride, String customFontFileNameInAssets) {
try {
final Typeface customFontTypeface = Typeface.createFromAsset(
context.getAssets(), customFontFileNameInAssets);
final Field defaultFontTypefaceField = Typeface.class
.getDeclaredField(defaultFontNameToOverride);
defaultFontTypefaceField.setAccessible(true);
defaultFontTypefaceField.set(null, customFontTypeface);
} catch (Exception e) {
System.out.println("Can not set custom font "
+ customFontFileNameInAssets + " instead of "
+ defaultFontNameToOverride);
}
}
}
In your style.xml inside your theme add for eg
<style name="MyMaterialTheme.Base"parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:typeface">serif</item>
</style>
Thats it
You need to create a folder named "fonts" in asset folder and put the fonts in them your font path should be like this assets/fonts/BElham.ttf
and then define the fonts like that
@Override
public void onCreate() {
super.onCreate();
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/BElham.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.