Reputation: 1081
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>
Upvotes: 2
Views: 1415
Reputation: 5266
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.
Upvotes: 2
Reputation: 14835
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()
);
}
Upvotes: 0
Reputation: 328
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 e.g
<style name="MyMaterialTheme.Base"parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:typeface">serif</item>
</style>
Thats it
Upvotes: 1