Custom Font in Android

This tutorial shows you, how to develop an application in your local language. Since some Android phones have problems with Unicode, you can use custom true-type fonts for your application. The same technique can be used to decorate your application using stylish English fonts as well. This tutorial develops an application to display a welcome message in Tamil. You can use your own font to display whatever you want.

Step 1:
Create an Android application “Custom Font” with a package name “com.javahelps.customfont”.

Step 2:
Add a TextView to the activity_main.xml as shown below.
<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=".MainActivity">

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

Step 3:
Right click on the “res” folder and select NewFolderAssets Folder.


Step 4:
Just click Finish on the appeared dialog.


Step 5:
Copy and paste any true-type font file into the assets folder. In this tutorial, a Tamil font baamini.ttf is used. (If you want to use the same font, right click on the above link and select the "Save link as..." option.)


When you insert the font file, make sure that the file name satisfies the Android resources naming standard.

Step 6:
Add the following code into the onCreate method of the MainActivity.
// Find the TextView object
TextView txtMessage = (TextView) findViewById(R.id.txtMessage);
// Create a TypeFace using the font file
Typeface typeface = Typeface.createFromAsset(getAssets(), "baamini.ttf");
// Set a text to the TextView
txtMessage.setText("tzf;fk;");  // Welcome in Tamil
// Set the typeface
txtMessage.setTypeface(typeface);
After the modification, onCreate method must look like this.
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Find the TextView object
    TextView txtMessage = (TextView) findViewById(R.id.txtMessage);
    // Create a TypeFace using the font file
    Typeface typeface = Typeface.createFromAsset(getAssets(), "baamini.ttf");
    // Set a text to the TextView
    txtMessage.setText("tzf;fk;");  // Welcome in Tamil
    // Set the typeface
    txtMessage.setTypeface(typeface);
}
Step 7:
Save all and run the application.


Find the project at Git Hub.
Previous
Next Post »

2 comments

Write comments
dsp
AUTHOR
July 13, 2016 at 9:04 AM delete

அருமை தலைவா!

Reply
avatar

Contact Form

Name

Email *

Message *