Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Button text font in android

I have a button created using android widgets. I want to set the font of the button text to Helv Neue 67 Med Cond. How to get this font and set it to the button text in android layout file?

like image 993
Ajn Avatar asked Jun 16 '11 13:06

Ajn


People also ask

How do I change the font on my button text?

To change the font size of a button, use the font-size property.

What font do buttons use?

Font. Sans-serif font is the standard for all kinds of buttons. That's because sans-serif fonts are very readable for every online usage.

How do I use OTF files on Android?

To change font styles in GO Launcher, copy the TTF or OTF font files on your phone. Long press on the home screen and select GO Settings. Choose Font–>Select Font. Pick the font you want or tap Scan to add files stored on your device.


5 Answers

I guess you may have already found the answer, but if not (and for other developers), you can do it like this:

1.you want to save the "Helv Neue 67 Med Cond.ttf" in to assets folder. then

For TextView

  TextView txt = (TextView) findViewById(R.id.custom_font);
  Typeface typeface = Typeface.createFromAsset(getAssets(), "Helv Neue 67 Med Cond.ttf");
  txt.setTypeface(typeface);

For Button

  Button n=(Button) findViewById(R.id.button1);
  Typeface typeface = Typeface.createFromAsset(getAssets(), "Helv Neue 67 Med Cond.ttf");
  n.setText("show");
  n.setTypeface(typeface);
like image 193
Ann Avatar answered Sep 17 '22 02:09

Ann


First you have to put the ttf file in assets folder and then You can use the below code to set Custom font in TextView, same way you can do for Button:

TextView txt = (TextView) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "Helv Neue 67 Med Cond.ttf");
txt.setTypeface(font);
like image 22
Paresh Mayani Avatar answered Sep 17 '22 02:09

Paresh Mayani


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

public class ButtonPlus extends Button {

    public ButtonPlus(Context context) {
        super(context);
        applyCustomFont(context);
    }

    public ButtonPlus(Context context, AttributeSet attrs) {
        super(context, attrs);
        applyCustomFont(context);
    }

    public ButtonPlus(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        applyCustomFont(context);
    }

    private void applyCustomFont(Context context) {
            Typeface customFont = FontCache.getTypeface("fonts/candy.ttf", context);
            setTypeface(customFont);
        }
    }

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

public class FontCache {

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

    public static Typeface getTypeface(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;
    }
}

And finally an example use in a layout:

 <com.my.package.buttons.ButtonPlus
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_sometext"/>

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.

Also you can see this tutorial and a example in GitHub.

like image 23
Cabezas Avatar answered Sep 17 '22 02:09

Cabezas


Android comes with 3 fonts (Sans, Serif, Monospace) which can be accesed using android:typeface=”FONT_NAME”.

For using custom font, you should use code like

TextView txt = (TextView) findViewById(R.id.custom_font);
  Typeface typeface = Typeface.createFromAsset(getAssets(), "Helv Neue 67 Med Cond.ttf");
  txt.setTypeface(typeface);

Some similar questions are Custom Fonts in Android and Android - Using Custom Font.

like image 42
Pankaj Kumar Avatar answered Sep 19 '22 02:09

Pankaj Kumar


You can use:

android:typeface="yourfont"
like image 20
BrainCrash Avatar answered Sep 19 '22 02:09

BrainCrash