Use custom Font in Android App

Nikhiljain08 July 14, 2017
0 people like this post
Custom Font Android

Today, we try to implement custom font in android applications without using any 3rd party library in the application. I have search so many websites and blog to find out how to use custom font in complete app but I am not able to find out a single post which covers complete app’s font change. Most of the post talks about TextView font change only but only few talks about changing font of other views also like EditText, Button, RadioButton, Tabs, etc.

I am going to show you how to use custom font in your complete android application. It will cover 95% of the view available in android.

So let’s start!

▶ Add you all custom fonts under Assets folder – “assets/fonts”
app/src/main/assets/fonts/

I am going to one font for four different style:

  1. OpenSans-Bold.ttf – For Bold text
  2. OpenSans-BoldItalic.ttf – For Bold & Italic text
  3. OpenSans-Italic.ttf– For Italic text
  4. OpenSans-Regular.ttf – For regular/normal text

 

▶ Create a class GlobalClass.java

GlobalClass.java

 

▶ Add setFont() function to GlobalClass class:

GlobalClass.java

 

▶ Creating views in activity_main.xml:

activity_main.xml

 

▶ Initialize GlobalClass class in MainActivity.java and call setFont() function by passing complete activity view to the function:

MainActivity.java

 

▶ Run the app emulator and check out the results:

We can find out the difference between two images.

 

▶ Lets add few more views to the class and check the results:

 

▶ Run the app emulator and find out the result:

We can see that the fonts of the views are almost changed. Also, the text entered in EditText view is also changed. Similarly, you can change font for any view by just passing view to the setFont() function defined in GlobalClass class. Don’t forget to initialize the GlobalClass in your main class. Use you custom font in app and check the results.

Please download complete code if required.

Hope this post will help you add custom font to your android app project.

 

Please do comments and let us know if any issues are there and you want to add custom font to your one of the view. 🙂

Category: Android
  • 0
  • 188
Nikhiljain08