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);
textview.setTypeface(...)

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;
		init();
	}

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

}

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" />
</declare-styleable>

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

<LinearLayout
  android:id="@+id/comments"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" >
 
  <com.parkuik.android.ui.CustomTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/no_comments" />
</LinearLayout>

 

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

Related Whitepaper:

Rapid Android Development: Build Rich, Sensor-Based Applications with Processing

Create mobile apps for Android phones and tablets faster and more easily than you ever imagined

Use 'Processing', the free, award-winning, graphics-savvy language and development environment, to work with the touchscreens, hardware sensors, cameras, network transceivers, and other devices and software in the latest Android phones and tablets.

Get it Now!  

2 Responses to "Android TextView with custom fonts"

  1. Bill Mote says:

    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. opasam says:

    Could you post your FontsUtils class please ?

Leave a Reply


six − 6 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books