In this Android custom font article, we will look at how to apply custom font for the entire Application.
Here I am taking the project of my previous article on FirebaseUI and cloning it to apply custom font to that application. The entire code for this tutorial is made available as a separate Github project. You can clone/download.from the following link:
1. Declare a class to Override Fonts:
We declare a class to override the fonts. Here I’ve named it as FontsOverride.java.
Here I’ve declared the above class as final so that no other subclasses can be created for it so that we are restricting the concept of inheritance for this class.
2. Placing the required android custom font in our Application:
The font I am going to use is Raleway.ttf which is an open source font which can be acquired at the following link.
Create an assets folder by changing our android perspective to project and navigating to src > main and right click on main and select new > Folder > Assets Folder
Now create a sub folder in assets folder named fonts and place Raleway.ttf in fonts folder.
3. Declare /Modify Application Subclass:
Android Application is the base class in Android that maintains Global Application State. We can write a subclass extending Application class and declare it in our AndroidManifest.xml‘s <application> tag. Android os will initialize the Application class or its subclass before any other classes when the process for your application/package is created. For more on Android Application class, you can visit the following link.
Here I am creating a class named CustomFontApp.java that extends Application class and add the following code to it:
If your app already contains a subclass that extends application, then modify that class by adding the following lines of code to it.
4. Declare the application tag in Android Manifest:
5. Apply typeface to styles.xml:
Now we will add the following line to our styles.xml so that the typeface will be applicable to our entire application.
Now my styles.xml will look like:
Now run the app to see that the Android custom font is applied to the entire application!