Kit Ramos
Kit Ramos

Reputation: 1859

change Default font for Android App

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?

Upvotes: 4

Views: 6108

Answers (3)

Wishwa Gayan
Wishwa Gayan

Reputation: 1

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>

Upvotes: 0

Vipul
Vipul

Reputation: 28093

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

Upvotes: 8

selva_pollachi
selva_pollachi

Reputation: 4217

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);

Upvotes: 2

Related Questions