4

Okay, I know how to change what font is used on individual textboxes in my app, but I want ALL the text in my app to use this custom font and color, and currently the only way I can think of to do his is to reference each box and set them to the proper font and color. while it's doable it seems rather a clunky method, is there a better way?

Kit Ramos
  • 1,301
  • 1
  • 13
  • 30
  • I think you had to code your own style and/or theme. See this useful answers: http://stackoverflow.com/questions/8787690/adding-custom-font-to-theme-in-android or http://stackoverflow.com/questions/4395309/android-want-to-set-custom-fonts-for-whole-application-not-runtime or maybe this http://stackoverflow.com/questions/2973270/using-a-custom-typeface-in-android . – JJ86 Jul 25 '13 at 14:07

3 Answers3

8

You can create custom TextView and reffer it everywhere.

public class TypefacedTextView extends TextView {

    public TypefacedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        Typeface typeface = Typeface.createFromAsset(context.getAssets(), fontName);
        setTypeface(typeface);
    }
}

Inside view.xml

<packagename.TypefacedTextView
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:text="Hello"/>
yincrash
  • 6,016
  • 1
  • 38
  • 40
Vipul
  • 31,716
  • 7
  • 69
  • 86
2

Put Your different font files in assets folder...

TextView mytextView=(TextView)findViewById(R.id.txtbx);
Typeface fonttype=Typeface.createFromAsset(getAssets(),"fonts/fontname.ttf");
mytextView.setTypeface(fonttype);
selva_pollachi
  • 4,117
  • 4
  • 27
  • 41
0

By Creating Font-Famly element in Font Resource file and adding your font family to the app theme

in res>values>styles>

----------
<resources>
<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <!--Add this below line-->
        <item name="android:fontFamily">@font/PacificoRegular</item>
    </style>
</resources>

vezunchik
  • 3,599
  • 3
  • 15
  • 25