0

I want to set a custom font in my app and done that by Is it possible to set a custom font for entire of application? and i have done it but the problem is when i apply AppTheme_1 it changes font for android version greater than or equal to lollipop and when i apply AppTheme_2 then it changes fonts for only android version less than lollipop. But I want to change the font for all android versions.

 <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar"> </style> <style name="AppTheme_1" parent="AppBaseTheme"> <!-- Customize your theme here. --> <item name="android:textAppearance">@style/CustomTextAppearance</item> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="android:windowNoTitle">true</item> <item name="android:windowFullscreen">true</item> <item name="windowNoTitle">true</item> <item name="windowActionBar">false</item> <item name="android:windowContentOverlay">@null</item> </style> <style name="CustomTextAppearance"> <item name="android:typeface">serif</item> </style> </style> <style name="AppTheme_2" parent="AppBaseTheme"> <!-- Customize your theme here. --> <item name="android:typeface">serif</item> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="android:windowNoTitle">true</item> <item name="android:windowFullscreen">true</item> <item name="windowNoTitle">true</item> <item name="windowActionBar">false</item> <item name="android:windowContentOverlay">@null</item> </style> 
1

2 Answers 2

2

We can achieve to set a single font to entire application using Calligraphy lib

Here is the library link: Calligraphy

Now add compile 'uk.co.chrisjenx:calligraphy:2.3.0' in your build.gradle

Now create an application class like follows:

public class MyApplication extends Application { private static MyApplication sInstance; public static MyApplication getInstance() { return sInstance; } @Override public void onCreate() { super.onCreate(); sInstance = this; CalligraphyConfig.initDefault(new CalligraphyConfig.Builder() .setDefaultFontPath("fonts/Roboto-Regular.ttf") .setFontAttrId(R.attr.fontPath) .build()); }} 

Now you need to call this application class in manifest file like follows in application tag

 android:name=".MyApplication" 

By doing this entire app have set one font. You can go through the library for better understanding.

Sign up to request clarification or add additional context in comments.

2 Comments

Can this method be used for external fonts (For eg- assests/fonts/chalk.ttf )?
@viditBabele you can use external fonts too.
0

Try this

MyApplication.java (This is Application class)

//Typeface public static Typeface poppinsSemiBold; public static Typeface helveticaNeueMedium; public static Typeface poppinsRegular; /** * Preload typefaces. */ private void preloadTypefaces() { poppinsSemiBold = Typefaces.get(getApplicationContext(), Typefaces.FONT_POPPINS_SEMI_BOLD); helveticaNeueMedium = Typefaces.get(getApplicationContext(), Typefaces.FONT_HELVETICANEUE_MEDIUM); poppinsRegular = Typefaces.get(getApplicationContext(), Typefaces.FONT_POPPINS_REGULAR); } 

call this preloadTypefaces() in onCreate() of same class MyApplication.java

Typefaces.java

import android.content.Context; import android.graphics.Typeface; import android.util.Log; import java.util.Hashtable; public class Typefaces { private static final String TAG = Typefaces.class.getSimpleName(); public static final String FONT_POPPINS_SEMI_BOLD = "fonts/Poppins_SemiBold.ttf"; public static final String FONT_HELVETICANEUE_MEDIUM = "fonts/HelveticaNeue_Medium.otf"; public static final String FONT_POPPINS_REGULAR = "fonts/Poppins_Regular.ttf"; private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>(); public static Typeface get(Context c, String assetPath) { synchronized (cache) { if (!cache.containsKey(assetPath)) { try { Typeface t = Typeface.createFromAsset(c.getAssets(), assetPath); cache.put(assetPath, t); } catch (Exception e) { Log.e(TAG, "Could not get typeface '" + assetPath + "' because " + e.getMessage()); return null; } } return cache.get(assetPath); } } } 

Make sure your .ttf or .otf file is in assets/fonts/HelveticaNeue_Medium.otf added like that

res/values/attrs.xml

<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CustomFontTextView"> <attr name="fontStyle" format="enum"> <enum name="poppinsSemiBold" value="1" /> <enum name="helveticaNeueMedium" value="2" /> <enum name="poppinsRegular" value="3" /> </attr> </declare-styleable> <declare-styleable name="TextViewPlus"> <attr name="customFont" format="string" /> </declare-styleable> </resources> 

Custom Font class TextViewPlus.java

import android.content.Context; import android.content.res.TypedArray; import android.graphics.Typeface; import android.util.AttributeSet; import android.util.Log; import android.widget.TextView; import com.offerbuk.R; import com.offerbuk.app.MyApplication; public class TextViewPlus extends TextView { private static final String TAG = "TextViewPlus"; private int fontStyle; private TypedArray a = null; public TextViewPlus(Context context) { super(context); init(null, 0); } public TextViewPlus(Context context, AttributeSet attrs) { super(context, attrs); init(attrs, 0); } public TextViewPlus(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(attrs, defStyle); } private void init(AttributeSet attrs, int defStyle) { try { this.a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomFontTextView, defStyle, 0); this.fontStyle = a.getInteger(R.styleable.CustomFontTextView_fontStyle, 0); } finally { if (this.a != null) this.a.recycle(); } if (!isInEditMode()) { switch (fontStyle) { case 1: setTypeface(MyApplication.poppinsSemiBold); break; case 2: setTypeface(MyApplication.helveticaNeueMedium); break; case 3: setTypeface(MyApplication.poppinsRegular); break; default: setTypeface(MyApplication.poppinsRegular); break; } } } } 

Use this in Your layout file

 <TextViewPlus android:layout_width="wrap_content" android:layout_height="wrap_content" card_view:fontStyle="poppinsRegular" /> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.