Garima Tiwari Garima Tiwari - 5 months ago 35
Android Question

How to set a particular font for a button text in android?

I want my button text to be in the Copperplate Gothic Light font and I yet have not come across a simple clean code for a simple function as this. Help!

PS: Since android comes with ariel and a few other fonts on its own we need to import (apologies for the lack of a better word since im new to this) the font we wish to use. This is all I have been able to gather till yet and this is where the trail ends for me.

Help needed! :]


If you plan to add the same font to several buttons I suggest that you go all the way and implement it as a style and subclass button:

public class ButtonPlus extends Button {

    public ButtonPlus(Context context) {

    public ButtonPlus(Context context, AttributeSet attrs) {
        super(context, attrs);
        CustomFontHelper.setCustomFont(this, context, attrs);

    public ButtonPlus(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        CustomFontHelper.setCustomFont(this, context, attrs);

This is a helper class to set a font on a TextView (remember, Button is a subclass of TextView) based on the attribute:

public class CustomFontHelper {

     * Sets a font on a textview based on the custom attribute
     * If the custom font attribute isn't found in the attributes nothing happens
     * @param textview
     * @param context
     * @param attrs
    public static void setCustomFont(TextView textview, Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomFont);
        String font = a.getString(R.styleable.CustomFont_font);
        setCustomFont(textview, font, context);

     * Sets a font on a textview
     * @param textview
     * @param font
     * @param context
    public static void setCustomFont(TextView textview, String font, Context context) {
        if(font == null) {
        Typeface tf = FontCache.get(font, context);
        if(tf != null) {


And here's the FontCache to reduce memory usage on older devices:

public class FontCache {

    private static Hashtable<String, Typeface> fontCache = new Hashtable<String, Typeface>();

    public static Typeface get(String name, Context context) {
        Typeface tf = fontCache.get(name);
        if(tf == null) {
            try {
                tf = Typeface.createFromAsset(context.getAssets(), name);
            catch (Exception e) {
                return null;
            fontCache.put(name, tf);
        return tf;

In res/values/attrs.xml we define the custom styleable attribute

<?xml version="1.0" encoding="utf-8"?>
    <declare-styleable name="CustomFont">
        <attr name="font" format="string"/>

And finally an example use in a layout:


And in res/values/style.xml

<style name="button" parent="@android:style/Widget.Button">
    <item name="">fonts/copperplate_gothic_light.TTF</item>

This may seem like an awful lot of work, but you'll thank me once you have couple of handfuls of buttons and textfields that you want to change font on.