--ADVERTISEMENT--

By default, android SDK allows us to use a set of fonts like sans, serif, mono space etc in android application using typeface attribute or property. Only default fonts may not be sufficient for your application. In this tutorial, you will learn to load custom/external fonts in your android app programmatically.

Using custom fonts in android application is very easy. You just need to add extra two line of code. But you need to download the required fonts from the internet and add it inside assets folder of your android app.

Here I have used custom font in android TextView. Like this you can use anywhere you need instead of normal font.

Related:
How to Use Font Awesome Icons in Android Application
How to Make Android TextView Text Selectable
How to Add a Library Project to Android Studio


Android Example: Loading External/Custom Fonts in Android App


Following is the step by step guide to use custom fonts in android application programmatically.

1. Create a new Android Project


Let’s start by creating a new project with following information.

Application name: Using Custom Font in Android
Company Domain: viralandroid.com
Package name: com.viralandroid.usingcustomfontinandroid
Minimum SDK: Android 2.2 (API 8 Froyo)

2. Adding Custom Fonts in Assets Folder


Create an assets fonder in your app’s main directory then copy and paste all .ttf font files which you are going to used inside assets folder.

How to Use Custom Fonts in Android Application

3. Modify XML Layout File


Open your XML layout file i.e. activity_main.xml and add a simple TextView which will look like this.

res/layout/activity_main.xml


4. Modify Java Activity File


Now open your java activities file i.e. MainActivity.java and add following code.

src/MainActivity.java

Following is the default content of strings.xml file.

res/values/strings.xml

Android Example: Loading External/Custom Fonts in Android App

That’s all. Now run your How to Use Custom Fonts in Android application.

Post a Comment

Powered by Blogger.