Android

Android Custom Fonts

Android Custom Fonts

Sometimes you might not want to use the default font and may need to create your own customized font. To use a customized font in your android application, you are required to download that font from the internet and you will have to put that font in the assets folder of your project. 

There are some steps that you need to follow to use a custom font in your application and these steps are described below:

1.Create an android application under your project – com.firstapp.ashulakhwan.greatlearning

2.Download the font that you want to use in your application and put that file in the assets folder of your project directory. 

3.Modify the main activity file that is named MainActivity.java in your project directory:

package com.firstapp.ashulakhwan.greatlearning;

import android.graphics.Typeface;
import android.support.v8.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
   TextView cf1,cf2;

   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
         
      cf1=(TextView)findViewById(R.id.textView3);
      cf2=(TextView)findViewById(R.id.textView4);

      Typeface font1= Typeface.createFromAsset(getAssets(), "font/font.ttf");
      cf1.setTypeface(font1);

      Typeface font2= Typeface.createFromAsset(getAssets(), "font/font1.ttf");
      cf2.setTypeface(font2);
   }
}

4.Modify the layout file – activity_main.xml with the following content:

<?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:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" 
   tools:context=".MainActivity">
   
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Great Learning"
      android:id="@+id/textView2"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true"
      android:textSize="42dp"
      android:textColor="#e34234" />
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Typeface"
      android:id="@+id/textView"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:textSize="40dp" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Great Learning"
      android:id="@+id/textView4"
      android:layout_below="@+id/textView3"
      android:layout_alignLeft="@+id/textView3"
      android:layout_alignStart="@+id/textView3"
      android:layout_marginTop="75dp"
      android:textSize="48dp" />
   

    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Great Learning"
      android:id="@+id/textView3"
      android:layout_centerVertical="true"
      android:textSize="50dp"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true" />
</RelativeLayout>

5. At last, you need to check the results of your application by running it in Android Emulator or an android device.

Top course recommendations for you

    Python Practice Codes
    1 hrs
    Beginner
    8.7K+ Learners
    4.39  (468)
    VLOOKUP in Excel
    1 hrs
    Beginner
    46.2K+ Learners
    4.57  (3737)
    Blockchain Process
    1 hrs
    Beginner
    7.1K+ Learners
    4.58  (719)
    GO Programming Language
    1 hrs
    Beginner
    6.9K+ Learners
    4.49  (578)
    Conditional Formatting in Excel
    1 hrs
    Beginner
    18.1K+ Learners
    4.52  (1770)
    Fibonacci Series in Python
    1 hrs
    Beginner
    2.2K+ Learners
    4.62  (98)
    Design App
    1 hrs
    Beginner
    18.5K+ Learners
    4.45  (1277)
    Pivot Tables in Excel
    1 hrs
    Beginner
    22.8K+ Learners
    4.57  (3005)
    Divide and Conquer Algorithms
    1 hrs
    Beginner
    2.4K+ Learners
    4.59  (416)
    QR Code Generator in Python
    1 hrs
    Beginner
    4.9K+ Learners
    4.39  (225)