Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add external fonts to android application

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.

like image 834
bHaRaTh Avatar asked Apr 12 '11 11:04

bHaRaTh


People also ask

How do I add custom fonts to an app?

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.

Where do I put TTF files on Android?

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.


2 Answers

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

like image 157
Zelimir Avatar answered Sep 27 '22 21:09

Zelimir


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*/);        }    } } 
like image 32
Jabbir Basha Avatar answered Sep 27 '22 19:09

Jabbir Basha