Android External Custom Font for TextView

By | December 3, 2015

While developing an android app you might see the need to use custom fonts for views or widget which holds text content like TextView and Button for some special purposes . In this tutorial we will see how to include the external typeface font in asset folder and then apply for views which holds text

android_custom_font

Style

Open res/styles.xml and modify as below.

file : styles.xml

<style name="TextStyle">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textSize">@dimen/text_size</item>
    <item name="android:layout_marginTop">@dimen/text_margin_top</item>
</style>

Dimens

Open res/dimens.xml and modify as below

file : dimens.xml

<resources>
    <dimen name="activity_padding">15dp</dimen>
    <dimen name="text_size">45sp</dimen>
    <dimen name="text_margin_top">30dp</dimen>
</resources>

XML Layout

Create a XML Layout file activity_main.xml with LinearLayout Containing Three TextViews in it .

file : activity_main.xml

<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:padding="@dimen/activity_padding"
    tools:context=".MainActivity">
    
    <TextView
        android:id="@+id/textview_01"
        style="@style/TextStyle"
        android:text="@string/blog_name" />

    <TextView
        android:id="@+id/textview_02"
        style="@style/TextStyle"
        android:text="@string/blog_name" />
    
    <TextView
        android:id="@+id/textview_03"
        style="@style/TextStyle"
        android:text="@string/blog_name" />

</LinearLayout>

Place TypeFace In Asset

First Add Asset Folder to your project , In android Studio Click on App > New > Folder > Asset Folder . This will create Asset Folder Inside the src/main .

android_asset

Now Place Custom Font Inside Asset Folder .

android_asset_folder

MainActivity

Create a class MainActivity and set the content of this class with above defined XML layout and then as you can see the highlighted in the below code which is very self explanatory for applying typeface.
Create a typeface instance by passing asset instance and path to custom typeface.
On the instance of TextView call setTypeface api by passing instance of typeface .

file : MainActivity.java

package com.tutorialsbuzz.customfont;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView mTextView_01 = null;
    private TextView mTextView_02 = null;
    private TextView mTextView_03 = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();

        Typeface face_01 = Typeface.createFromAsset(getAssets(), "customfont/libertylegion.ttf");
        mTextView_01.setTypeface(face_01);

        Typeface face_02 = Typeface.createFromAsset(getAssets(), "customfont/ChronicDelivery.ttf");
        mTextView_02.setTypeface(face_02);
        
        Typeface face_03 = Typeface.createFromAsset(getAssets(), "customfont/MilasianMediumPERSONAL.ttf");
        mTextView_03.setTypeface(face_03);
        
    }

    private void initViews() {
        mTextView_01 = (TextView) findViewById(R.id.textview_01);
        mTextView_02 = (TextView) findViewById(R.id.textview_02);
        mTextView_03 = (TextView) findViewById(R.id.textview_03);
    }
}

androidcustomfont_typeface