Tutorials Android Using External Fonts in Android View

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. Follow below simple steps to create an example that uses custom font on TextView.

  1. At first, download a font of your choice. Note that android supports .ttf extension. Here I have downloaded Maximum.ttf file.
  2. 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
TextView textview = (TextView) findViewById(R.id.textView);
Typeface font = Typeface.createFromAsset(getContext().getAssets(), "fonts/Maximum.ttf");
textView. setTypeface(font);
  1. 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.
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);
	}
}
  1. Now change the following in your android layout xml file
<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"
    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

[download src=”https://github.com/javatechig/Android-UI-Tutorials/tree/master/AndroidCustomFont”]
Total
0
Shares
guest
10 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
iTechniqz
iTechniqz
9 years ago

For more Android Tutorials

Visit ANDROIDITUTS

http://www.androidituts

javatechig
javatechig
9 years ago
Reply to  iTechniqz

We do not entertain such links. thanks

farooq
farooq
8 years ago

Thanksssssssssssssssss

careful7j
careful7j
8 years ago

Dat awesome thing, however if you try to use it listView you will get a terrible performnace. To fix it, make font a static variable to it not call on assets up on every textView.

Nilanchala Panigrahy
Nilanchala Panigrahy
7 years ago

Currently android studio doesn’t support real time ttf rendering. You must run in simulator or device to test.

Rafael Anastacio Alves
Rafael Anastacio Alves
7 years ago

Same for Eclipse?

Nilanchala Panigrahy
Nilanchala Panigrahy
7 years ago

yes. pretty much same for eclipse.

sardasht aziz
sardasht aziz
7 years ago

well done

purna
purna
7 years ago

the assets folder should be created under main. It is not in res.

Aslam Hossin
Aslam Hossin
6 years ago

It’s working properly in lower version of android but upper version like Lollipop nothing has been changed. How can I do this in Lollipop and other upper device ?

Previous Post
Android Tutorials

Handler and AsyncTask in Android

Next Post
Android Tutorials

ProgressBar while Loading WebView in Android

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
10
0
Would love your thoughts, please comment.x
()
x