Custom font settings for all TextViews

Is there an easy way to make all text objects (and any other text elements in my application) use my own custom font (and not built-in options) without having to manually set them using textview.setTypeface ()?

I suppose the Textview extension will do the trick, but then creating interfaces with a visual editor is a pain. I was thinking of a bit of styling, but I can't find how to set my own font there.

+6
source share
5 answers

If you need to install one font for all TextViews in an Android application, you can use this solution. It will override all TextView fonts, include an action bar and other standard components, but the EditText password font will not be exceeded.

Myapp.java

public class MyApp extends Application { @Override public void onCreate() { TypefaceUtil.overrideFont(getApplicationContext(), "SERIF", "fonts/Roboto-Regular.ttf"); } } 

TypefaceUtil.java

  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("TypefaceUtil","Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride); } } 

}

themes.xml

  <?xml version="1.0" encoding="utf-8"?> <resources> <style name="MyAppTheme" 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> 

Update for Android 5.0 or higher

As I researched and tested on a device running api 5.0 or higher, this solution works fine, because I use a single style.xml file and do not make another style.xml in the values-21 folder

Although, Some users ask me that this solution does not work with Android device version 5.0 (they can use -21 style.xml values, I think)

So, this is due to the fact that in API 21 most text styles include fontFamily setting, e.g.

 <item name="fontFamily">@string/font_family_body_1_material</item> 

Uses the default Roboto Regular font:

 <string name="font_family_body_1_material">sans-serif</string> 

So the best solution is

If someone has a problem not working on 5.0+. Do not override the font in your v21 value styles.

Just redefine the font in /style.xml values ​​and it will be useful for you :)

+12
source

You can create a method to set the font for the text view in the general class, and you can set the call method and send the text view as its attribute.

+1
source

Yes. Just create a style and set it to a specific font, and then install the entire application in that style.

0
source

You can do this by subclassing the TextView and calling the setTypeFace method. For example, in the constructor.

0
source

Create a method in the Constants class so that it can be obtained from all fragments and actions:

  public static void overrideFonts(final Context context, final View v) { try { if (v instanceof ViewGroup) { ViewGroup vg = (ViewGroup) v; for (int i = 0; i < vg.getChildCount(); i++) { View child = vg.getChildAt(i); overrideFonts(context, child); } } else if (v instanceof TextView) { ((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "font/Lato-Regular.ttf")); } else if (v instanceof EditText) { ((EditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "font/Lato-Regular.ttf")); } else if (v instanceof Button) { ((Button) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "font/Lato-Regular.ttf")); } } catch (Exception e) { } } 

Method call in action as:

  Constants.overrideFonts(MainActivity.this, getWindow().getDecorView().getRootView()); 

Method call in fragment as:

  Constants.overrideFonts(getActivity(), view); 
0
source

Source: https://habr.com/ru/post/952192/


All Articles