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.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
We are using XML layout for our application's UI and we are creating two textviews with ids text1 and text2.
Now copy the following code in ExternalFont.java
package com.botskool.ExternalFont;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;
public class ExternalFont extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Typeface font1 = Typeface.createFromAsset(getAssets(), "fonts/RockFont.ttf");
Typeface font2 = Typeface.createFromAsset(getAssets(), "fonts/FEASFBRG.TTF");
TextView customText1 = (TextView)findViewById(R.id.text1);
TextView customText2 = (TextView)findViewById(R.id.text2);
customText1.setTypeface(font1);
customText1.setTextSize(40.f);
customText1.setText("Hello! This is a custom font...");
customText2.setTypeface(font2);
customText2.setTextSize(30.f);
customText2.setText("Developed by www.bOtskOOl.com");
}
}
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.
Run your Android Application.
Enjoy.
Do post your doubts, queries or suggestions in our forum.
Download ExternalFont.apk.