I was looking for some stylish fonts for my android application. but the problem is how can i make my android application supportable for external fonts.
Thank you.
The android:fontFamily attribute of the TextView class is used to specify the font. Step 1: Go to the XML file and go to the Design view. Step 2: Click the TextView you want to change the font of. Step 3: In the search bar, search for fontFamily.
Create a folder in the main directory called Fonts. The main directory should be the one where your Download folder is. Use your file browser again to find your downloaded TTF file. Copy and paste it into the Fonts folder.
You need to create fonts folder under assets folder in your project and put your TTF into it. Then in your Activity onCreate()
TextView myTextView=(TextView)findViewById(R.id.textBox); Typeface typeFace=Typeface.createFromAsset(getAssets(),"fonts/mytruetypefont.ttf"); myTextView.setTypeface(typeFace);
Please note that not all TTF will work. While I was experimenting, it worked just for a subset (on Windows the ones whose name is written in small caps).
You can use the custom TextView for whole app with custom font here is an example for that
public class MyTextView extends TextView { Typeface normalTypeface = Typeface.createFromAsset(getContext().getAssets(), Constants.FONT_REGULAR); Typeface boldTypeface = Typeface.createFromAsset(getContext().getAssets(), Constants.FONT_BOLD); public MyTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public MyTextView(Context context, AttributeSet attrs) { super(context, attrs); } public MyTextView(Context context) { super(context); } public void setTypeface(Typeface tf, int style) { if (style == Typeface.BOLD) { super.setTypeface(boldTypeface/*, -1*/); } else { super.setTypeface(normalTypeface/*, -1*/); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With