Text is one of the most important part of any application or game regardless of the platform it is running on. Without good fonts your overall design doesn’t finish up looking neat.
I was recently making and designing a small fun app for Android (find it here) and I opted to use paper/pencil sort of design for it. I happily designed some backgrounds with paper textures (in GIMP) but eventually I faced a bad situation since Android doesn’t have a font (natively available) that gives a sketchy/handwriting type of look to the text. Without the fonts I liked the app to have, the overall feel of the application would be ruined. I was using libgdx for the main part but one of the screens was build with Android views and layouts. Libgdx does support custom fonts and integrating it was smooth. The problem aroused when I tried to use the same font for the views I was using in the other activity. After a lot of experimenting and surfing on the internet, I got it to work!
So, here’s what you need to do in order to use custom fonts in your apps. Before beginning, let me make this clear that font files are incorporated in your app’s apk. This means that the more larger the font file (.ttf) is the larger your apk will become.
- First step – Choose a font! Remember, the font should not be large enough that results in drastically increasing the apk size.
- Then, you need to copy its TTF file into the assets folder of your app/game. You can directly put it into this folder or you can also keep a separate folder inside the assets for fonts.
- Declare a Typeface instance either globally or inside the function in which you’ll assign this to your controls.
- Next, initialize this variable by calling Typeface.createFromAsset function like below:
- I made a fonts folder inside the assets folder and the same was specified in the function argument. I found people using and suggesting createFromFile function instead of createFromAsset but that simply didn’t work out for me. So this is what I will recommend to my readers.
- Now that you have the Typeface initialized, you can simply assign it to any view that has something to do with fonts or text (EditText, TextView etc). You need to use setTypeface function of the view in order to assign the typeface to that view.
And that’s it! Do get back to me with your queries or if you have other method of achieving this objective in Android apps/games.