Home > Android > Android Using External Fonts

Android Using External Fonts


Loading external fonts in your android application is very easy. It can be achieved with only two lines of code.

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)

android using external fonts

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="ghost" />
</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.androidhive;
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/Face Your Fears.ttf";
        // text view label
        TextView txtGhost = (TextView) findViewById(R.id.ghost);
        // Loading Font Face
        Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
        // Applying font
        txtGhost.setTypeface(tf);
    }
}

5. Run your project.

android loading external fonts
Source: androidhive.info
Categories: Android
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: