70

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.

7 Answers 7

145

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

Sign up to request clarification or add additional context in comments.

8 Comments

where is this assets folder? I am using android studio and I can find it in under src/
It is not under src, assets folder should be under your main project folder (at the same level as: src, res, gen, bin...) Check: developer.android.com/tools/projects/index.html
@Zelimir isn't there a way to dis universally for the project? and not take each TextView and set it's typeface?
@libathos - sounds reasonable, but I am not sure how to do that. Did not have time to investigate, have you?
@libathos well you can define a Typeface through xml but that actually requires more writing than having it in coding
|
12

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*/); } } } 

Comments

9

Create a folder named fonts in the assets folder and add the snippet from the below link.

Typeface tf = Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/fontname.ttf"); textview.setTypeface(tf); 

Comments

7

To implement you need use Typeface go through with sample below

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Roboto/Roboto-Regular.ttf"); for (View view : allViews) { if (view instanceof TextView) { TextView textView = (TextView) view; textView.setTypeface(typeface); } } } 

1 Comment

for allViews you have to loop through the layout using getChildCount() so you could just replace the foreach with a simple for loop.
5

The easiest way to accomplish this is to package the desired font(s) with your application. To do this, simply create an assets/ folder in the project root, and put your fonts (in TrueType, or TTF, form) in the assets. You might, for example, create assets/fonts/ and put your TTF files in there.

Then, you need to tell your widgets to use that font. Unfortunately, you can no longer use layout XML for this, since the XML does not know about any fonts you may have tucked away as an application asset. Instead, you need to make the change in Java code, by calling Typeface.createFromAsset(getAssets(), “fonts/HandmadeTypewriter.ttf”), then taking the created Typeface object and passing it to your TextView via setTypeface().

For more reference here is the tutorial where I got this:

http://www.androidguys.com/2008/08/18/fun-with-fonts/

Comments

2

I recommend this approach it very nice with adding name of custom font in typeface to styles.xml and putting your set of fonts into assets folder.

1 Comment

Very good approach, I suggest to take it to an attention. And maybe is a good idea to copy main code and post here, if link won't be accessible in the future.
2

One more point in addition to the above answers. When using a font inside a fragment, the typeface instantiation should be done in the onAttach method ( override ) as given below:

@Override public void onAttach(Activity activity){ super.onAttach(activity); Typeface tf = Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/fontname.ttf"); } 

Reason:
There is a short span of time before a fragment is attached to an activity. If CreateFromAsset method is called before attaching fragment to an activity an error occurs.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.