Home » Android » Android Core » Android TextView with custom fonts

About Javier Manzano

Javier is an Android and node.js developer. Working day to day on new pet projects. Founder of Parkuik and Codeely.

Android TextView with custom fonts

While developing an android app I found that everytime I was trying to use a custom font on a TextView I copied the same lines:

TextView textview = (TextView) findViewById(R.id.text);

Obviously, that’s unnecessary and repetitive. Why not just create a custom view? And more, why not adding the font through xml code?

public class CustomTextView extends TextView {

  private Context mContext;
	private String mFont;
	public CustomTextView(Context context) {
		super(context, null);
		mContext = context;

	public CustomTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		TypedArray a = context.getTheme().obtainStyledAttributes(
		        0, 0);
		try {
			mFont = a.getString(R.styleable.CustomButtom_font);
		} finally {
	private void init() {
		if (mFont != null) {


We just have to extend from TextView and read from the attribute set the font string declared on its styleable resource. Just create an attrs.xml (or use an existing one) and add the following:

<declare-styleable name="CustomTextview">
  <attr name="font" format="string" />

Now, you could declare on your xml layout like this:

  android:layout_height="wrap_content" >
    android:text="@string/no_comments" />


Reference: Android TextView with custom fonts from our JCG partner Javier Manzano at the Javier Manzano’s Blog blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....



  1. You didn’t include your FontsUtils() class in which, I assume, you’re actually doing the Typeface.createFromAsset() call to get the font from fonts/font_name.*tf

  2. Could you post your FontsUtils class please ?

  3. from where did we get “CustomButtom_font” ?

    thank you

Leave a Reply

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


Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.