Android Custom Fonts Tutorial With Examples

In this tutorial, we’ll explain how to set up android custom fonts in TextViews and Buttons in our application.

Android Custom Fonts

Android SDK comes with a set of standard fonts that can be styled by using a few XML attributes. Let’s look at them.

Let’s club android:fontFamily and android:textStyle and display the different variants of a TextView.

The styles.xml which contains the different style variants is given below.

The activity_main.xml is given below.

To set custom fonts on a TextView/Button/EditText object, the Typeface property is used.
The Typeface class specifies the typeface and intrinsic style of a font.
To set a typeface over a TextView we invoke the method setTypeface().
Following are the Typeface constants that can be used.

  • BOLD
  • BOLD_ITALIC
  • ITALIC
  • NORMAL

To set custom fonts on our Views, we first need to import the font files into our project.
Font files generally come up in two types .ttf (True Type Font) and .otf (Open Type Font).

Android Custom Fonts Project Structure

android-custom-fonts-project-structure
In the above image a new folder assets has been added under the main directory.

Android Custom Fonts Code

We’ve created another layout namely activity_custom_fonts.xml for setting custom fonts on our Views.

We’re setting our custom fonts in the java code. The MainActivity.java is given below

In the above code we’re calling createFromAsset() method on the Typeface class to create a new instance of the Typeface. An instance of the application’s AssetManager is called by passing getAssets() as the first parameter. The custom font asset file path is passed as a string in the second parameter. Since we’ve placed the font files in the root of assets directory, passing the font filenames would suffice.

Note: Setting external fonts using android:typeface doesn’t work

We end up with the following output when the application is run.

android custom fonts output

Isn’t it a time consuming and redundant task to set the typeface individually for each of the views?
We need a better alternative. Our answer lies in Custom Views. In the next section, we’ll be creating a Custom TextView class and define custom XML attributes to set the external fonts in the XML layout itself.

Our Updated Project Structure is given below

android-custom-fonts-project-structure

Before we implement our CustomTextView.java, let’s look into the TypeFactory.java class as shown below.

The above code essentially creates a custom typeface font for each of the font files. We can now use the class variables in our CustomTextView to set the font accordingly.

The CustomTextView.java class is given below.

R.styleable.CustomTextView is defined inside the attrs.xml file as shown below.

The attribute font_name is the custom attribute we’ll be using in the XML layout.

Our updated activity_custom_fonts.xml layout is given below:

The MainActivity.java code now would just require setting the Button typeface.

Note: A CustomButton Custom View can be created in a similar way. Try that out!

This brings an end to this tutorial. You can download the Android CustomFonts Tutorial from the below link.

By admin

Leave a Reply