Change font of entire application in android

In android you can give your own customs fonts for all screens and widgets in the entire application. For achieving that you need to download the required fonts into your applications assets/fonts folder. So the first thing you want to do after creating the project is to create an assets folder.

  1. click on file->new->folder->Assets Folder
  2. A popup will come, In that click on finish button without checking “change folder location”.
  3. In assets folder will get created, Again right -click on it and create another directory inside assets and name it as “fonts”
  4. Put your custom font inside this folder
  5. Now create a file in java/your package/TypefaceUtil.java

Here is the code for TypefaceUtil.java

TypefaceUtil.java

public class TypefaceUtil {

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("serif", 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);
}
}
}
}

6. Create a file in java/your package/CustomFontApp.java, this class will extend as Application

Here is the code for CustomFontApp.java

CustomFontApp.java

public class CustomFontApp extends Application {
@Override
public void onCreate() {
super.onCreate();

TypefaceUtil.overrideFont(getApplicationContext(), "SERIF", "fonts/OpenSans-SemiboldItalic.ttf");

}
}

7. Next step is to register our custom Application to AndroidManifest.xml file. So your the application tag in manifest will be :

<application
    android:name=".CustomFontApp"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
   >

8.Finally add the following line to your styles.xml to the style which you are reffering in
AndroidManifest.xml

<item name="android:typeface">serif</item>

Above you saw to how give custom font to the entire android application.

Suppose if you just want to change only a single strings font , then you need to specifically setTypeface to that widget.

First you need to define your custom font in your activity by

Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-SemiboldItalic.ttf");

now use the below code for your textview(can be button or any other view contains text)

your_textview.setTypeface(custom_font );

 

 

About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

2 Comments

  1. Sathish Kumar January 16, 2019 Reply

Add a Comment

Your email address will not be published. Required fields are marked *