0

Thanks to this post, I know you can add fonts in an activity and can use it within that class. Using:

 TextView hindiTextView=(TextView)findViewById(R.id.txtbx);
 Typeface hindiFont=Typeface.createFromAsset(getAssets(),"fonts/fontname.ttf");
 mytextView.setTypeface(hindiFont);

But what if I want to add a font in a single place and use it in the entire applicaiton? (Like in manifest or some property file)

Community
  • 1
  • 1
reiley
  • 3,759
  • 12
  • 58
  • 114
  • No by default android does not have such way for custom fonts. You can write a utility class and put the code related to get custom typeface from there. – Pankaj Kumar Oct 11 '13 at 06:31
  • And if you are planning to use custom fonts and you can design your custom components then http://vision-apps.blogspot.in/2012/02/android-better-way-to-apply-custom-font.html should be best start. – Pankaj Kumar Oct 11 '13 at 06:36

3 Answers3

1

Actually there is not way to implement common custom font for whole application. Checkout HERE Which is to say: no, you can't set a font for the entire application.

But still if you want to try out ,try as below which is referenced HERE:

Common fonts class:

public final class FontsOverride {

    public static void setDefaultFont(Context context,
            String staticTypefaceFieldName, String fontAssetName) {
        final Typeface regular = Typeface.createFromAsset(context.getAssets(),
                fontAssetName);
        replaceFont(staticTypefaceFieldName, regular);
    }

    protected static void replaceFont(String staticTypefaceFieldName,
            final Typeface newTypeface) {
        try {
            final Field StaticField = Typeface.class
                    .getDeclaredField(staticTypefaceFieldName);
            StaticField.setAccessible(true);
            StaticField.set(null, newTypeface);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}

You then need to overload the few default fonts, for example:

FontsOverride.setDefaultFont(this, "DEFAULT", "MyFontAsset.ttf");
FontsOverride.setDefaultFont(this, "MONOSPACE", "MyFontAsset2.ttf");
FontsOverride.setDefaultFont(this, "SANS_SERIF", "MyFontAsset3.ttf");

Or course if you are using the same font file, you can improve on this to load it just once.

Community
  • 1
  • 1
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
0

No, sorry. You can only add custom fonts using the code you've just written.

amatellanes
  • 3,645
  • 2
  • 17
  • 19
0

Create a custom class which extends TextView , add the font logic their . Instead of using android TextView in layout , use <com.mypackage.myTextView android:id="@+id/myid />

Rahul Patil
  • 2,707
  • 2
  • 21
  • 32