How to use Custom made fonts in Android?

In this article, we are going to learn about custom fonts and then use them as an example with three text views in Android.
Submitted by Manu Jemini, on February 01, 2018

In the example below, we are going to use the Graphics.Typeface class to create animation. This static method Typeface.createFromAsset() will attach a custom font which can be used to create a customize TextView.

To implement this in your program, you will need to import: android.graphics.Typeface

In your java file, create a layout file with three TextViews like this one:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Font 1"
        android:id="@+id/textView1"
        android:textSize="45dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />

We would set the fontSize and also use wrap content property in the TextViews.

First we will take the reference of the TextViews from the layout file.

This is how we will store the reference: T1=(TextView)findViewById(R.id.textView1);

The Static method of the 'TypeFace' class will be used on the TextViews: Typeface face1= Typeface.createFromAsset(getAssets(),"Fonty.ttf");

1) Java file:

package com.example.hp.myapplication;

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

public class MainActivity extends AppCompatActivity {
    TextView T1,T2,T3;

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

        T1=(TextView)findViewById(R.id.textView1);
        T2=(TextView)findViewById(R.id.textView2);
        T3=(TextView)findViewById(R.id.textView3);

        Typeface face= Typeface.createFromAsset(getAssets(),"Bubblesfont-Regular.ttf");
        T1.setTypeface(face);

        Typeface face1= Typeface.createFromAsset(getAssets(),"Fonty.ttf");
        T2.setTypeface(face1);

        Typeface face2= Typeface.createFromAsset(getAssets(),"doridrobot.ttf");
        T3.setTypeface(face2);
    }
}

2) XML file:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hp.myapplication.MainActivity">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Font 1"
        android:id="@+id/textView1"
        android:textSize="45dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Font 2"
        android:id="@+id/textView2"
        android:layout_below="@+id/textView1"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignStart="@+id/textView1"
        android:layout_marginTop="73dp"
        android:textSize="45dp"
        android:layout_alignParentEnd="true" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Font 3"
        android:id="@+id/textView3"
        android:layout_below="@+id/textView2"
        android:layout_alignLeft="@+id/textView2"
        android:layout_alignStart="@+id/textView2"
        android:layout_marginTop="73dp"
        android:textSize="45dp"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

3) Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output

Android - Custom made font

Related Tutorials



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.