app/src/main/java/com/pinkal/todolist/customfont/TypefaceUtil.java
/**
     * 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
     */


@param context                    to work with assets
@param defaultFontNameToOverride  for example "monospace"
@param customFontFileNameInAssets file name of the font from assets
@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) {
        final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(), customFontFileNameInAssets);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Map<String, Typeface> newMap = new HashMap<String, Typeface>();
            newMap.put("avenir", customFontTypeface);
            try {
                final Field staticField = Typeface.class
                        .getDeclaredField("sSystemFontMap");
                staticField.setAccessible(true);
                staticField.set(null, newMap);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        } else {
            try {
                final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride);
                defaultFontTypefaceField.setAccessible(true);
                defaultFontTypefaceField.set(null, customFontTypeface);
            } catch (Exception e) {
                Log.e(TypefaceUtil.class.getSimpleName(), "Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride);
            }
        }
    }
public static void overrideFont ( Context context , String defaultFontNameToOverride , String customFontFileNameInAssets ) { final Typeface customFontTypeface = Typeface . createFromAsset ( context . getAssets ( ) , customFontFileNameInAssets ) ; if ( Build . VERSION . SDK_INT >= Build . VERSION_CODES . LOLLIPOP ) { Map < String , Typeface > newMap = new HashMap < String , Typeface > ( ) ; newMap . put ( "avenir" , customFontTypeface ) ; try { final Field staticField = Typeface . class . getDeclaredField ( "sSystemFontMap" ) ; staticField . setAccessible ( true ) ; staticField . set ( null , newMap ) ; } catch ( NoSuchFieldException e ) { e . printStackTrace ( ) ; } catch ( IllegalAccessException e ) { e . printStackTrace ( ) ; } } else { try { final Field defaultFontTypefaceField = Typeface . class . getDeclaredField ( defaultFontNameToOverride ) ; defaultFontTypefaceField . setAccessible ( true ) ; defaultFontTypefaceField . set ( null , customFontTypeface ) ; } catch ( Exception e ) { Log . e ( TypefaceUtil . class . getSimpleName ( ) , "Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride ) ; } } }
Using reflection to override default typeface
 NOTICE: DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE WHICH WILL BE OVERRIDDEN
Using reflection to override default typeface NOTICE : DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE WHICH WILL BE OVERRIDDEN