Setting Custom Font Through XML With DataBinding

Today, we will learn about setting custom font through XML with data binding. We already know how to set custom font through Java code.
  1. Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/font name.ttf");  
  2. tv.setTypeface(custom_font);  
It will set font type for the given TextView or other View but what will be the case, if we have more number of TextViews/EditTexts throughout the Application and that also with multiple custom fonts? We need to declare those number of objects for TextView/EditText as well as for TypeFace declaration. Don’t you think it will increase the number of codes? Yes, the other option will come in your mind is to create Custom TextView/EditText. Not a bad idea, but we have 15-20 custom fonts throughout the app, so creating those number of Custom class would realy be a bad idea. Thus, what is the simplest and easies solution? Answer is data binding.

With the help of data binding, you will be able to set custom font with just one line and that too without creating any object of TextView/EditText. Doesn't it sound interesting? Let’s follow the step by step instructions to implement it.

Step 1 - Implement data binding in your Android Studio project.

Refer to my previous tutorial to setup DataBinding “Working with DataBinding Android“.

Step 2 - Create custom class (CustomFontFamily) for storing and accessing custom fonts.
  1. public class CustomFontFamily {  
  2.     static CustomFontFamily customFontFamily;  
  3.     HashMap <String, String> fontMap = new HashMap<>();  
  4.     public static CustomFontFamily getInstance() {  
  5.         if (customFontFamily == null)  
  6.             customFontFamily = new CustomFontFamily();  
  7.         return customFontFamily;  
  8.     }  
  9.     public void addFont(String alias, String fontName) {  
  10.         fontMap.put(alias, fontName);  
  11.     }  
  12.     public Typeface getFont(String alias) {  
  13.         String fontFilename = fontMap.get(alias);  
  14.         if (fontFilename == null) {  
  15.             Log.e("""Font not available with name " + alias);  
  16.             return null;  
  17.         } else {  
  18.             Typeface typeface = Typeface.createFromAsset(CustomApplication.getContext().getAssets(), "fonts/" + fontFilename);  
  19.             return typeface;  
  20.         }  
  21.     }  
  22. }  
Step 3 - Define custom fonts in Application class.
  1. public class CustomApplication extends Application {  
  2.     private static Context context;  
  3.     CustomFontFamily customFontFamily;  
  4.     @Override  
  5.     public void onCreate() {  
  6.         super.onCreate();  
  7.         CustomApplication.context = this;  
  8.         customFontFamily = CustomFontFamily.getInstance();  
  9.         // add your custom fonts here with your own custom name.  
  10.         customFontFamily.addFont("amatic""AmaticSC-Regular.ttf");  
  11.         customFontFamily.addFont("pacific""Pacifico.ttf");  
  12.         customFontFamily.addFont("seasrn""SEASRN.ttf");  
  13.         customFontFamily.addFont("capture""Capture_it.ttf");  
  14.         customFontFamily.addFont("xcelsion""Xcelsion_Italic.ttf");  
  15.     }  
  16.     public static Context getContext() {  
  17.         return context;  
  18.     }  
  19. }  
Note - All the fonts, defined in Application class, must be available under assets/fonts folder.

available

Step 4 - Write FontBinding class to use fonts throughout the application.
  1. public class FontBinding {  
  2.     @BindingAdapter({"bind:font"})  
  3.     public static void setFont(TextView textView, String fontName) {  
  4.         textView.setTypeface(CustomFontFamily.getInstance().getFont(fontName));  
  5.     }  
  6. }  
Step 5 - Access fonts from XML.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <layout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.         xmlns:app="http://schemas.android.com/apk/res-auto">  
  4.   
  5.     <LinearLayout android:layout_width="match_parent" 
  6.                   android:layout_height="match_parent" 
  7.                   android:gravity="center" 
  8.                   android:orientation="vertical">  
  9.   
  10.         <TextView android:layout_width="wrap_content" 
  11.                   android:layout_height="wrap_content" 
  12.                   android:text="Welcome to AndroidGig" 
  13.                   android:textAllCaps="true" 
  14.                   android:textAppearance="?android:attr/textAppearanceLarge" 
  15.                   app:font="@{`amatic`}" />  
  16.   
  17.         <TextView android:layout_width="wrap_content" 
  18.                   android:layout_height="wrap_content" 
  19.                   android:layout_marginTop="10dp" 
  20.                   android:text="Welcome to AndroidGig" 
  21.                   android:textAllCaps="true" 
  22.                   android:textAppearance="?android:attr/textAppearanceLarge" 
  23.                   app:font="@{`pacific`}" />  
  24.   
  25.         <TextView android:layout_width="wrap_content" 
  26.                   android:layout_height="wrap_content" 
  27.                   android:layout_marginTop="10dp" 
  28.                   android:text="Welcome to AndroidGig" 
  29.                   android:textAllCaps="true" 
  30.                   android:textAppearance="?android:attr/textAppearanceLarge" 
  31.                   app:font="@{`seasrn`}" />  
  32.   
  33.         <TextView android:layout_width="wrap_content" 
  34.                   android:layout_height="wrap_content" 
  35.                   android:layout_marginTop="10dp" 
  36.                   android:text="Welcome to AndroidGig" 
  37.                   android:textAllCaps="true" 
  38.                   android:textAppearance="?android:attr/textAppearanceLarge" 
  39.                   app:font="@{`capture`}" />  
  40.   
  41.         <TextView android:layout_width="wrap_content" 
  42.                   android:layout_height="wrap_content" 
  43.                   android:layout_marginTop="10dp" 
  44.                   android:text="Welcome to AndroidGig" 
  45.                   android:textAllCaps="true" 
  46.                   android:textAppearance="?android:attr/textAppearanceMedium" 
  47.                   app:font="@{`xcelsion`}" />  
  48.     </LinearLayout>  
  49.   
  50. </layout>  
Step 6 - The final step is to bind your activity with a View.
  1. @Override  
  2. protected void onCreate(Bundle savedInstanceState) {  
  3.     super.onCreate(savedInstanceState);  
  4.     DataBindingUtil.setContentView(this, R.layout.activity_main);  
  5. }  
available

The code, given above, will work perfectly but it will create an object of TypeFace each time we set the font. What we will do next is to store that TypeFace in cache; so next time, it will take TapeFace from cache instead of creating a new object. There will be a minor change you need to do in your CustomFontFamily.java.
  1. public class CustomFontFamily {  
  2.     static CustomFontFamily customFontFamily;  
  3.     HashMap <String, String> fontMap = new HashMap<>();  
  4.     HashMap <String, Typeface> fontCache = new HashMap<>();  
  5.     public static CustomFontFamily getInstance() {  
  6.         if (customFontFamily == null)  
  7.             customFontFamily = new CustomFontFamily();  
  8.         return customFontFamily;  
  9.     }  
  10.     public void addFont(String alias, String fontName) {  
  11.         fontMap.put(alias, fontName);  
  12.     }  
  13.     public Typeface getFont(String alias) {  
  14.         String fontFilename = fontMap.get(alias);  
  15.         if (fontFilename == null) {  
  16.             Log.e("""Font not available with name " + alias);  
  17.             return null;  
  18.         }  
  19.         if (fontCache.containsKey(alias))  
  20.             return fontCache.get(alias);  
  21.         else {  
  22.             Typeface typeface = Typeface.createFromAsset(CustomApplication.getContext().getAssets(), "fonts/" + fontFilename);  
  23.             fontCache.put(fontFilename, typeface);  
  24.             return typeface;  
  25.         }  
  26.     }  
  27. }  
Now, whenever there is a new TypeFace object created, it will be stored in HashMap. Thus, from next time, it will be taken from map object.
Next Recommended Reading Recycler View With Android DataBinding