Using Custom Fonts with Android

How to use custom fonts in your project

This demo will teach us how to use custom fonts in the android project. Follow the below steps to integrate custom fonts into your android project.

Step 1: Create New Project


Create New Project

Step 2: Add Your Fonts To the Project



Create a folder named fonts under the assets folder and put all your fonts in that folder. (Folder name can be anything)
 

Step 3: Open the layout file and add TextView

&ltRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"&gt
    
    &ltandroid.support.v7.widget.RecyclerView
      android:id="@+id/rvContacts"
      android:layout_width="match_parent"
      android:layout_height="match_parent" /&gt

   &ltTextView
        android:id="@+id/txt_custom"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="@string/hello_world" /&gt

&lt/RelativeLayout&gt
    


Step 4: Open Your Activity Class file and try the following code. 

Here, we will get fonts from the project directory using the Typeface class and then apply those fonts to TextView.
public class MainActivity extends Activity {
 
TextView txt_custom;
Typeface tface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        tface=Typeface.createFromAsset(getAssets(),"fonts/future.ttf");
        txt_custom=(TextView)findViewById(R.id.txt_custom);
        txt_custom.setTypeface(tface);
    }
}
Now, Compile and run the project to see the result. You can see Textview fonts have been changed from regular fonts. See the below image for an example. 

Output:



Thanks for reading this article. Hope you would have liked it!. Please share and subscribe to this blog to support. 

Pragnesh Ghoda

A forward-thinking developer offering more than 8 years of experience building, integrating, and supporting android applications for mobile and tablet devices on the Android platform. Talks about #kotlin and #android

Post a Comment

Please let us know about any concerns or query.

Previous Post Next Post

Contact Form