Monday, April 27, 2015

How to add custom fonts to the textview in android application

In Android, you can set your custom fonts to the textviews in your application.  When you want to use the different language fonts, it really helps. I was working on an android application for SRSM Mantralaya i.e. Panchanga application. That time we wanted to add the Kannada language support to the application. This was the solution I used and it worked well. You need to have the font file for the language or the fonts you want to use in the application. Generally this will be the .ttf file.

You need to place this font file in assets/font folder of the android application. After putting fonts in the assets folder under “font” folder, you can access it in your java code through Typeface class.

First, get the reference of the text view in the code. Its syntax is given below:

The next thing you need to do is to call static method of Typeface class createFromAsset() to get your custom font from assets. The syntax is given below:

Here the font_name is the name of the font file you copied to asset/font folder.
The last thing you need to do is to set this custom font object to your TextView Typeface property. You need to call setTypeface() method to do that. The  syntax is given below:

That’s it. Try it. Happy Coding.

1 comment:

Pavithra M said...

It is really a great work and the way in which u r sharing the knowledge is excellent.
Thanks for helping me to understand basic concepts. As a beginner in android programming your post help me a lot.Thanks for your informative article. Android Training in velachery | Android Training institute in chennai