Unable to change default font in Android application

I am trying to change the default font in my application. But that does not work. These are the steps I took:

1) The created TypefaceUtil.java class

import android.content.Context; import android.graphics.Typeface; import android.util.Log; import java.lang.reflect.Field; public class TypefaceUtil { 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("CustomFontException", "Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride); } } } 

2) In the class extending the Application :

 public void onCreate() { super.onCreate(); TypefaceUtil.overrideFont(getApplicationContext(), "MONOSPACE", "fonts/varelaround_regular.ttf"); } 

3) In styles.xml

  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="android:typeface">monospace</item> </style> 

Not working yet. Did I miss something?

+5
source share
3 answers

I ran into this problem once. I'm not very sure why it works, but you can try the following: Instead of "monospace", try each of them: DEFAULT, SANS_SERIF, SERIF. This may not work for all text views, for example, in ListView or recyclerView (strange, right?). But in those cases, I install the font programmatically from the adapter. Sorry for not being able to explain the reason.

+2
source

For this purpose, I highly recommend that you use Calligraphy , https://github.com/chrisjenx/Calligraphy , a terrific library that is actually hidden for changing the default font with it and has many other useful features.

All you need to install should be in the Readme.

+1
source

Why not define your custom TextView and assign it a font.

 import android.content.Context; import android.graphics.Typeface; import android.util.AttributeSet; import android.widget.EditText; import com.exmple.util.Utility; public class CustomFontEditText extends EditText { private Context mContext; private String ttfName; String TAG = getClass().getName(); public CustomFontEditText(Context context) { super(context); this.mContext = context; } public CustomFontEditText(Context context, AttributeSet attrs) { super(context, attrs); this.mContext = context; // Typeface.createFromAsset doesn't work in the layout editor. // Skipping... if (isInEditMode()) { return; } for (int i = 0; i < attrs.getAttributeCount(); i++) { this.ttfName = attrs.getAttributeValue(Utility.ATTRIBUTE_SCHEMA, Utility.ATTRIBUTE_TTF_KEY); if (null != ttfName) init(); } } public CustomFontEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.mContext = context; } private void init() { Typeface font = Utility.getFonts(mContext, ttfName); if (null != font) setTypeface(font); } @Override public void setTypeface(Typeface tf) { super.setTypeface(tf); } } 

Download and save the font from the resource in a hash map in your Utility class so that you can reuse the font later. Imagine loading a font every time a new View loads. OOM .. !!!

 private static Map<String, Typeface> TYPEFACE = new HashMap<String, Typeface>(); public static Typeface getFonts(Context context, String fontName) { Typeface typeface = TYPEFACE.get(fontName); if (typeface == null) { typeface = Typeface.createFromAsset(context.getAssets(), "fonts/"+fontName); TYPEFACE.put(fontName, typeface); } return typeface; } 

Add ttf attribute to set font from xml

 public static final String ATTRIBUTE_TTF_KEY = "ttf_name"; public static final String ATTRIBUTE_SCHEMA = "http://schemas.android.com/apk/lib/com.exmaple.ui.customfont"; 

and now use in your layout file

  <com.example.ui.customviews.CustomFontEditText xmlns:font="http://schemas.android.com/apk/lib/com.exmaple.ui.customfont" android:id="@+id/edt_pay_input" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:background="@null" android:cursorVisible="false" android:drawableBottom="@drawable/dashed_white" android:includeFontPadding="false" android:inputType="numberDecimal" android:maxLength="4" android:minWidth="20dp" android:singleLine="true" android:textColor="@color/white" android:textSize="30sp" font:ttf_name="FedraSansStd-Light.otf" > <requestFocus /> </com.example.ui.customviews.CustomFontEditText> 

You can do the same for Button, TextView, etc.

0
source

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


All Articles