How to use external fonts in Android

In this tutorial we will show how you can use external fonts in your Android applications instead of regular available fonts.

The first step is that you need to download a free font (.ttf file) from internet which you want to use. It's easy. Go to google.com and search for free fonts and download the one which you want to use. Here in this tutorial we will be using two fonts - RockFont font and FeastOfFlesh font as shown below.

Create a new Android Project. This project will have the following properties -

Project Name - ExternalFont

Application Name - How to use custom fonts in Android

Package Name - com.botskool.ExternalFont

Activity Name - ExternalFont

Now inside the assets folder of your project create a new folder and name it as fonts. Copy the font(.ttf) files in this folder. In our case the font files are RockFont.ttf and FEASFBRG.TTF.

If you are using Eclipse refresh your project directory tree once. Now copy the following code in the main.xml.

The Typeface class specifies the typeface and intrinsic style of a font. We have created two objects of this class - font1 and font2 and assigned them the path of two font files by using creatFromAsset() method.

After this we declare two objects of TextView class and assign them the ids of our XML layout. We then define what type of font to use by using setTypeface() function. We can alter the default textsize by using setTextSize() method. And finally we define the text to be displayed via setText() method.