Getting Started in Android Development
Part 27 -
There may be times when you wish to use a custom font over the default font for Android devices. This tutorial looks at the straightforward way this can be done.
The first task is to make sure you have an ‘Assets’ folder. This will need to be create manually in Android Studio, but is created automatically in Eclipse. To do this in Android Studio simply right click over the ‘app’ folder in the project view and select ‘new assets folder’. This is the place to then store any font files. I tend to create a subfolder under this called ‘fonts’ and place them there.
A simple layout was created for this tutorial with a single TextView. The code for this layout is below:
All we need to do to change the font is create three line of code:
TextView font = (TextView) findViewById(R.id.tvFont);
Typeface customFont = Typeface.createFromAsset(getAssets(), "fonts/handwriting.ttf");
The Typeface associates the font file in the assets folder to the variable customFont. It is then simply a case of setting the type face of the TextView.
The resultant screenshot is below:
After this tutorial your FontActivity.java file should look similar to the one below:
After this tutorial your AndroidManifest file should look similar to the one below:
After this tutorial your ListViewActivity.java file should look similar to the one below:
Download Download tutorial set 1