External Fonts

Web Hosting

Android Using External Fonts

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.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#222222" >
        android:text="ghost" />
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.
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 {
    public void onCreate(Bundle savedInstanceState) {
        // 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
5. Run your project.

Web Hosting

No comments:

Post a Comment