Thursday 5 September 2013

Using external font in Android Typeface Example

Loading external fonts in android is very easy

1.Add your external fonts in assets folder 

2.Create  a Class Typeface_Utils

import android.app.Activity;
import android.graphics.Typeface;

public class Typeface_Utils {
   
    static Typeface tf;
    public static void Typeface_font(Activity activity)
    {
        //add new font and use it .settypeface(tf);
        tf = Typeface.createFromAsset(activity.getAssets(),"abc.ttf");
    }
}

3.Then we will create the layout as follows:


<RelativeLayout 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" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Typeface_Sample"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

4.Then in your MainActivity.

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

public class MainActivity extends Activity {

    TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        tv = (TextView)findViewById(R.id.textView1);
        Typeface_Utils.Typeface_font(MainActivity.this);
        tv.setTypeface(Typeface_Utils.tf);
    }

}






No comments:

Post a Comment