Android

Using Custom Fonts In Android Application Development

Using of custom fonts in android application development is not yet welcomed and we know fonts are major factor in defining the look and feel of User Interface. Android in the beginning was not giving any option to add custom fonts and it only shows the default font .i.e, the same font of the device. From API level 16 they were providing an option with 3 other fonts called

List Of Default Fontfamily

  • DEFAULT
  • DEFAULT_BOLD
  • MONOSPACE
  • SANS_SERIF
  • SERIF

These above mentioned list of fonts may not satisfy an Android UI developer because converting mockup into xml based UI components definitely needs custom fonts.

Let’s Change The Font Of A TextView

It can be easily done by few steps of codes. I will explain it in detail. See the code snippet below,

Create Asset Folder

Create a folder named assets under MyApplication > app > src > main

create_asset_folder

Place File With Extension ttf In Asset Folder

place_ttf_file_in_asset_fol

Create Utility Class

Let’s create a class called FontUtils.java

package app.devdeeds.com.myapplication;

import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Hashtable;

public class FontUtils {

    private static final Hashtable<String, Typeface> cache = new Hashtable<>();
    private Typeface mCustomTypeFace;

    public FontUtils(Context context) {
        mCustomTypeFace = get(context, "LDFComicSans");
        //if you want to use more than one custom font then declare all of them here like above.
    }

    public static Typeface get(Context c, String name) {
        synchronized (cache) {
            if (!cache.containsKey(name)) {
                Typeface t = Typeface.createFromAsset(c.getAssets(),
                        String.format("%s.ttf", name));
                cache.put(name, t);
            }
            return cache.get(name);
        }
    }

    public TextView changeFontOfTextView(View view) {

        TextView objTextView = (TextView) view;
        objTextView.setTypeface(this.mCustomTypeFace);
        return objTextView;
    }

    public EditText changeFontOfEditText(View view) {

        EditText objEditText = (EditText) view;
        objEditText.setTypeface(this.mCustomTypeFace);
        return objEditText;

    }

    public Button changeFontOfButton(View view) {

        Button objButtonView = (Button) view;
        objButtonView.setTypeface(this.mCustomTypeFace);
        return objButtonView;
    }
}

Create Activity Layout File

Just create a simple TextView with any text

<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/sampleTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:text="HI FROM DEVDEEDS!!"
        android:textColor="#000"
        android:textSize="25sp" />

</RelativeLayout>

Finally Create Activity Class

Here we use our pre-created FontUtility Class. It will change the font family of the TextView with single function call. Inflate the TextView with FontUtility.

package app.devdeeds.com.myapplication;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;

public class MainActivity extends ActionBarActivity {

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

        FontUtils fontUtils = new FontUtils(getApplicationContext());
        fontUtils.changeFontOfTextView(findViewById(R.id.sampleTextView));
    }
}

When We Run This Code

screen_shot_custom_font

In this article i have showed you how to change the font family and use a custom font of a TextView. You can also change the font of any Android UI widget which has a writable text on it (EditText, Button, Switch etc)

If you want to ask anything about this topic please send me your comments through below comment box.
Best of luck!

About author

Rojer is a programmer by profession, but he likes to research new things and is also interested in writing. Devdeeds is his blog, where he writes all the blog posts related to technology, gadgets, mobile apps, games, and related content.

Leave a Reply

Your email address will not be published. Required fields are marked *