Android Custom Fonts

 

So far we were using default fonts provided by Android. Its time to try android custom fonts so that we can make a rich and vivid application interface. Adding android custom fonts is very easy, just we need to add a typeface from MainActivity.java file and link it to our TextView.


 

DownloadSource Code : Custom Fonts.zip


 

Now Let us work on Project : (Refer – Configuring Existing AndroidStudioProject) Custom Fonts which you might have downloaded from above or you can follow the scripts below by copying it to implement  in your Project. To launch Downloaded project, Extract the zip, copy it into AndroidStudioProjects. Now from Android Studio click open Existing Android Studio Project and select the path to extracted file we just copied in AndroidStudioProjects directory. If Sync SDK path prompt comes just click OK and you are ready to work. If still you get error check for AndroidStudioProjects/CustomFonts/app/build.gradle , it will contain different SDK Version. Change it to SDK Version which you have.

 

  • Extract FontStalemate.zip & copy stalemate-regular.ttf  into ‘assets‘ inside your Project-Custom Fonts as shown in below figure.


Android Custom Fonts - Assets

Now we have to utilize our font inside our MainActivity.java using type-face. This is shown in below code-snippet.

 

1. MainActivity.java

Explaining above Code :

  • Line 10 & 17 We have declared TextView, and linked them with TextView of (activity_main.xml)Layout file.
  • Line 21 We make customFont ( instance of class Typeface ) and add our font to it ( to its right we have attached stalemate-regular.ttf font which we have copied inside assets)  Syntax :  Typrface.createFromAsset(getAssets(), “stalemate-regular.ttf”);
  • Line 22 Finally we set our TextView with the above typeface so that we can have custom font for the TextView using syntax :  textView1 . setTypeface( customFont ) .

 

2. activity_main.xml : Layout file

 

We have added a simple TextView. We don’t have to code anything extra from our activity_main.xml and you are ready to go.

 

Using Android Custom Font we can develop our App supporting various languages other than English like Indian languages, Japanese,  German, Chinese, also your own mother-tongue. Just find a suitable TTF / OTF font and learn typing in our language & you are ready to go for your multilingual Language App. ( Remember that even with same language but different (font-style) some specific characters may differ, be precise in such ASCI code conversions ).

 

 

Final Output :


Android Custom Fonts

 

Do It Yourself :

Add some more TextView and apply custom fonts to them. Try making application supporting your mother-tongue. You can try other fonts which are inside ‘assets’.

Report Errors + Bugs & Become Insider for Nestedif.com

We would like to hear you, if you find any error or misspelled phrase while reading our tutorials. By reporting mistakes through email to insider@nestedif.com you could help other peers.