In this article, we will learn how to apply custom fonts to Views in Android like Toolbar, MenuItem apart from the views like TextView, EditText.
FontUtils:
FontUtils is a tiny font utility library used to apply custom fonts to Views. It supports the following views.
- Toolbar
- NavigationView
- Menu
- Submenu
- Other Views like EditText, TextView, etc.
This library is support the following Languages.
- Android – Java
- Android – Kotlin
- Android
Coding Part:
Steps:
I have split this part into 3 steps as in the following.
Step 1: Creating New Project with Empty Activity.
Step 2: Setting up the Library for Android Views.
Step 3: Applying Custom fonts to Android Views.
Step 1:Creating New Project with Empty Activity
- Open Android Studio and Select Create new project.
- Name the project as your wish and select Empty activity.
- Click “finish” button to create new project in Android Studio.
Step 2:Setting up the Library for Android Views
In this part, we will see how to setup the library for the project.
- Open your app level build.gradle file and add the following lines to download the library.
… implementation 'com.ajts.androidmads.fontutils:fontutils:1.0.1' …
- Then Click “Sync Now” to download the library.
- We need to use “compile” instead of “implementation” if we have using Android Studio Version below 3.0.
Step 3:Applying Custom fonts to Android Views
In this part, we will see how to apply custom fonts to Views. Here, we have to use “Typeface” and to more about “TypeFace” Click here.
- In this article, I am assigning the fonts from Assets folder. To create assets folder, right on the app folder and select New. Then select Folder and click assets folder.
- Then Copy and Paste the fonts you want to use with your application.
- The following line shows how to initialize the library.
// Applying Custom Font Typeface typeface = Typeface.createFromAsset(getAssets(), "custom_font.ttf"); // Init Library FontUtils fontUtils = new FontUtils();
- Then Initialize your Views and apply the fonts to the Views. The following example shows how to apply the fonts to Toolbar of your application.
// Apply font to Toolbar fontUtils.applyFontToToolbar(toolbar, typeface);
- You can apply custom fonts to other views like NavigationView, Menu, Submenu, TextView like in following example.
// Apply font to NavigationView fontUtils.applyFontToNavigationView(navigationView, typeface); // Apply font to Menu fontUtils.applyFontToMenu(menu, typeface); // Apply font to Other Views like TextView... fontUtils.applyFontToView(textview, typeface); fontUtils.applyFontToView(editText, typeface); fontUtils.applyFontToView(radioButton, typeface); fontUtils.applyFontToView(checkBox, typeface);
For Kotlin
We need to do the same steps mentioned. But we need to include Kotlin support while creating the project. Apply font to Views in Kotlin as show below.
// Applying Custom Font
val typeface = Typeface.createFromAsset(assets, "custom_font.ttf")
// Init Library
val fontUtils = FontUtils()
// Apply font to Toolbar
fontUtils.applyFontToToolbar(toolbar, typeface)
// Apply font to NavigationView
fontUtils.applyFontToNavigationView(navigationView, typeface)
// Apply font to Menu
fontUtils.applyFontToMenu(menu, typeface)
// Apply font to Other Views like TextView...
fontUtils.applyFontToView(textview, typeface)
fontUtils.applyFontToView(editText, typeface)
fontUtils.applyFontToView(radioButton, typeface)
fontUtils.applyFontToView(checkBox, typeface)
For Xamarin.Android
We can apply the same with Xamarin.Android. To know more Click Here.
Download Code
The article is informative do like and star the repo in GitHub. You can download the code here.
Note:Java and Kotlin Android Supporthttps://github.com/androidmads/FontUtilsXamarin.Android Support https://github.com/Xamarin-Gists/XAFontUtilsLibrary