Like the article? You may like us on Facebook or follow us on Twitter.

Using External Fonts in Android View

This tutorial explains example for using external fonts in Android View. Android applications are capable of loading the external font files with .ttf extension.

To create your view using the custom font, first download a font of your choice. Note that android supports .ttf extension. Here I have downloaded “Maximum.ttf” file. Create a new folder “font” in your assets folder and paste your recently downloaded font file.
We can change the font settings for a view simply by adding two lines of code

1
2
3
TextView textview = (TextView) findViewById(R.id.textView);
Typeface font = Typeface.createFromAsset(getContext().getAssets(), "fonts/Maximum.ttf");
textView. setTypeface(font);

This solution will work for you, but sometimes you may have to maintain the theme font across the application for each of the text view you use. You can do that by just extending your class from android TextView. Below is the code to my customized TextView.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.javatechig.droid.ui;
 
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
 
public class CustomTextView extends TextView {
	public CustomTextView(Context context) {
		super(context);
		setFont();
	}
	public CustomTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		setFont();
	}
	public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		setFont();
	}
 
	private void setFont() {
		Typeface font = Typeface.createFromAsset(getContext().getAssets(),
				"fonts/Maximum.ttf");
		setTypeface(font, Typeface.NORMAL);
	}
}

Now change the following in your android layout xml file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="#0F0F0F">
 
<com.javatechig.droid.ui.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/font_sample_text"
android:textSize="18dp"
android:textColor="#AAD4DE" />
 
<com.javatechig.droid.ui.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/font_sample_text"
android:textColor="#FFDE46"
android:textSize="24dp"/>
 
<com.javatechig.droid.ui.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/font_sample_text"
android:textSize="30dp"
android:textColor="#7FBA00"/>
 
</LinearLayout>

Here is the output of the above code

You can find the eclipse project from git

Download Code

About Nilanchala

A blogger, bit of a tech freak and a software developer. Follow him on Twitter, or like our official Facebook page or Google Plus.

  • farooq

    Thanksssssssssssssssss

  • iTechniqz

    For more Android Tutorials

    Visit ANDROIDITUTS

    http://www.androidituts

    • http://javatechig.com/ javatechig

      We do not entertain such links. thanks