mansi_singhal

java code for FontsOverride

Jul 25th, 2018
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. ###### java code for FontOverride
  2. package com.example.shikhadwivedi.homeautomation;
  3.  
  4. /**
  5. * Created by shikha on 9/7/18.
  6. */
  7.  
  8. import android.content.Context;
  9. import android.graphics.Typeface;
  10. import java.lang.reflect.Field;
  11.  
  12.  
  13. public class FontsOverride {
  14. public static void setDefaultFont(Context context,
  15. String staticTypefaceFieldName, String fontAssetName) {
  16. final Typeface regular = Typeface.createFromAsset(context.getAssets(),
  17. fontAssetName);
  18. replaceFont(staticTypefaceFieldName, regular);
  19. }
  20.  
  21. protected static void replaceFont(String staticTypefaceFieldName,
  22. final Typeface newTypeface) {
  23. try {
  24. final Field staticField = Typeface.class
  25. .getDeclaredField(staticTypefaceFieldName);
  26. staticField.setAccessible(true);
  27. staticField.set(null, newTypeface);
  28. } catch (NoSuchFieldException e) {
  29. e.printStackTrace();
  30. } catch (IllegalAccessException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. }
Add Comment
Please, Sign In to add comment