We downloaded a font and saved it in our app’s assets folder. We then use the font in our app:
- Directly in java where we apply the font to the TextView
- In XML where we apply the font referencing our custom TextView class where we specify the custom font as the default font
Here’s a screenshot:
The top row of text uses the default font while the bottom two rows use our custom Chubby Cheeks font
Get the font
There are plenty of internet sites offering fonts that you can download and use in your apps. Make sure:
- of the legal implications. Check the copyright to see that you are legally entitled to use the font
- that the font is in the True Type Font format – check for the .ttf extension
Download the font and save it in your app’s assets folder.
Don’t have an assets folder?
You’ll have to create an assets folder if you don’t have one.
In Android Studio, select File>app>New>Java Class>Folder>Assets Folder:
Select the Target Source Set and press Finish and Android Studio creates the folder for you.
Create a folder for the fonts
Now create a folder called fonts in the assets folder.
And there it is:
Put your custom font file in the assets>fonts folder
Save your custom font file in the fonts folder.
Doing it in java
You can now use your custom fonts directly in java.
First get a reference to the TextView displaying the text that you want to apply your custom font to. Then call the TypeFace class’s createFromAsset() method to create a new type face using your custom font. You pass an instance of the app's AssetManager as the first parameter by calling getAssets(). This enables you to access the custom font asset file which you pass as the second parameter.
Finally, call setTypeFace() to apply your custom font to the text.
Create a new TypeFace using your custom font, chubby.ttf which is saved in the assets/fonts folder
Doing it in xml
You can also apply the custom font to text that you define in an XML file.
First, create a custom TextView class where you create a new TypeFace using the custom font.
Create a custom TextView class
Create a custom TextView class where you create a new TypeFace using your custom font
A quick explanation
The constructor receives two parameters:
- the context
- a set of XML attributes. These will be applied to the new TextView
The new custom TextView is then constructed, using the custom TypeFace which is built using the custom font.
We now define our custom TextView in the XML layout file, specifying its attributes:
We define our custom TextView in the XML layout file
A quick explanation
Set the root element to our custom TextView, using the full package name. Then specify the attributes that you wish to apply to this particular TextView.
We’ve included the shadow attributes for fun. You can see more on shadows in our TextView tutorial, Playing with Android’s TextViews.
So where are the default fonts?
Have a look in your SDK folder for the platforms folder. Open it up and you should see the folders for the different platforms that are installed in your SDK. Here are mine:
An example of the platforms installed in an SDK
Here are the fonts included in the android-8 platform folder (they’re found in the android-8>data>fonts folder):
The fonts included in the android-8 platform folder
Thanks to James Shields for his great Chubby Cheeks font set.
I hope that you have found this tutorial helpful.
This project was created using Android Studio.