How to include External fonts in Android application


Fonts make the app look great with other UI elemenst but an obivious thing is that android provide not more fonts for your project so you need to add external fonts to your projects .
In this example we will see how to add External fonts to your project.

Start a new Project

1. Create a new project and fill the required details. File ⇒ New Project.
2. Create a folder called fonts under assets folder and place all your fonts in it. (Folder name can be anything)

3. Open your main.xml and create a simple textview.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#222222" >

    <TextView
        android:id="@+id/ghost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:textSize="70dip"
        android:gravity="center"
        android:textColor="#ef0000"
        android:layout_marginTop="50dip"
        android:text="Externalfont" />

</LinearLayout>


4. Now open your MainActivity class file and try following code. In the following code i am importing font from assets folder and using TypeFace class i am applying custom font on textview label.
AndroidExternalFontsActivity.java
package com.example.androidgreeve;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidExternalFontsActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Font path
        String fontPath = "fonts/Externalfont.ttf";

        // text view label
        TextView txtGhost = (TextView) findViewById(R.id.ghost);

        // Loading Font Face
        Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);

        // Applying font
        txtExternalfont.setTypeface(tf);
    }
}


5. Run your project.


Alll the world is a laboratory to the inquiring mind! Join Us

Comments

Alexfollows said…
The variable name in last line should rather be txtGhost and not txtExternalFont.
Good tutorial : )

Popular posts from this blog

A Deep Intro to Machine Learning(Python) - A Beginners Guide

Data visualization in python matplotlib with example

Pandas in Python for Data Analysis with Example(Step-by-Step guide)