Change default font for Android app

Ok, I know how to change which font is used in individual text blocks in my application, but I want ALL the text in my application to use this custom font and color, and currently the only way I can think of is to make it is to refer to each box and set them to the correct font and color. while this is doable, this seems like a pretty awkward method, is there a better way?

+4
source share
3 answers

You can create a custom TextView and damage 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"/> 
+8
source

Put your different font files in the resource folder ...

 TextView mytextView=(TextView)findViewById(R.id.txtbx); Typeface fonttype=Typeface.createFromAsset(getAssets(),"fonts/fontname.ttf"); mytextView.setTypeface(fonttype); 
+2
source

By creating a Font-Famly element in a Font Resource file and adding a font family to the application 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> 

0
source

Source: https://habr.com/ru/post/1493449/


All Articles