Android Tutorial

(avery) #1

By : Ketan Bhimani


266 

Some typefaces and typeface styles available on Android.

Using Custom Typefaces

You can easily use custom typefaces with
your application by including the font file
as an application asset and loading it on
demand. Fonts might be used for a
custom look-and feel, for implementing
language symbols that are not supported
natively, or for custom symbols.

For example, you might want to use a
handy chess font to implement a simple,
scalable chess game. A chess font includes
every symbol needed to implement a
chessboard, including the board and the
pieces. Hans Bodlaender has kindly provided a free chess font
called Chess Utrecht. Using the Chess Utrecht font, the letter Q
draws a black queen on a white square, whereas a q draws a white
queen on a white square, and so on.

To use a custom font, such as Chess Utrecht, simply download the
font from the website and copy the chess1.ttf file from your hard
drive to the project directory /assets/fonts/chess1.ttf. Now you can
load the Typeface object programmatically much as you would any
resource:

import android.graphics.Typeface;
import android.graphics.Color;
import android.graphics.Paint;
...
Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
Typeface mType =
Typeface.createFromAsset(getContext().getAssets(),
“fonts/chess1.ttf”);

Free download pdf