-
-
Save Aracem/91e570926e93a1532e88 to your computer and use it in GitHub Desktop.
Example to override the default font of an Application.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class OverrideTypefaceApplication extends Application { | |
@Override | |
public void onCreate() { | |
TypefaceUtil.overrideFont(getApplicationContext(), "SERIF", "fonts/Roboto-Regular.ttf"); // font from assets: "assets/fonts/Roboto-Regular.ttf | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<style name="CustomTheme" parent="@android:style/Theme.Holo.Light"> | |
<!-- you should set typeface which you want to override with TypefaceUtil --> | |
<item name="android:typeface">serif</item> | |
</style> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.graphics.Typeface; | |
import android.util.Log; | |
import java.lang.reflect.Field; | |
public class TypefaceUtil { | |
/** | |
* Using reflection to override default typeface | |
* NOTICE: DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE WHICH WILL BE OVERRIDDEN | |
* @param context to work with assets | |
* @param defaultFontNameToOverride for example "monospace" | |
* @param customFontFileNameInAssets file name of the font from assets | |
*/ | |
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) { | |
Log.e("Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride); | |
} | |
} | |
} |
#tjohnn I am facing same issue. Any solutions?
This solution fails for API level 26 and above.
Solution is to follow these steps https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml , instead of overriding fonts.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Thanks for this, it is working well for my application when the targetSdkVersion was API level 24, I upgraded to level 26 today and it is not working anymore. Do you have idea what is going wrong please?