Working with Fonts BLACKBERRY

The first and easiest thing to do will be to change the font used in the UI components. The BlackBerry platform includes pretty good font support and makes it almost trivial to change the font used for a component or an entire screen. Font support is provided through the net.rim.device.api.ui.Font and FontFamily classes. Through these, you can create fonts using any of the fonts installed on your device (quite a collection for all recent BlackBerry devices).

There are two ways to get a font. One is to obtain a specific font family (what might be called a typeface in different systems) and get a specific font from it. The other is to derive a font from another font you already have. To get font from a font family, we must have an instance of that font family; this just involves the FontFamily.forName method. You can use any of the names of the families on your device – you can see these on the device by going to the device’s options screen, and selecting Screen/Keyboard. This also gives you a nice real-time preview of different font families, styles, and sizes.

A selection of the font families available on a BlackBerry device

A selection of the font families available on a BlackBerry device

To demonstrate BlackBerry font support, we’ll explicitly set a new font for UiFun. Starting with the code as at the end of Chapter 4, add the following imports to the top of

The Ui class will be used in a minute; we’ve just added the import statement here to save time. We’ll use the BB Alpha Serif family, available on all devices, to more closely match the lettering in our logo image. Add the following to the UiFunMainScreen constructor:

This method won’t ever actually throw a ClassNotFoundException, though it’s a checked exception so the Java language requires us to add some code to handle it. If you specify a name for a font family that isn’t available, the BlackBerry will still return a default. There are a few choices for font style (the standard bold, italic, underlined, and so on), defined as constants in the Font class. You can also specify font sizes in a few different ways; the main ones to be concerned with are points and pixels. The size unit is specified using one of the constants from the net.rim.device.api.ui.Ui class. For our application, we’ll use a 9-point plain (not bold, italic, or underlined) version of BB Alpha Serif:

Font appFont = alphaSerifFamily.getFont(Font.PLAIN, 9, Ui.UNITS_pt);

NOTE:Font sizes should generally be specified as points instead of pixels, because BlackBerry devices vary a great deal in screen resolution and physical size, or in other words, in dots per inch (DPI). A 10-pixel font may be acceptable on a BlackBerry Pearl but will look tiny on the higher resolution screen of a Curve 8900. Using points makes the fonts appear roughly the same physical size on these different devices.

Changing the screen’s font is straightforward:


All the font code, added to UiFunMainScreen’s constructor, looks like this:

Each screen, manager, and field can have a different font, but setting the font for a container (screen or manager) will generally have the effect of setting the font for everything contained within it, unless you specify a different font for some of the components using their setFont methods. All this means that we don’t have to do anything else, and we’ve now specified a different font for all the controls in our application. Run UiFun, and you’ll see the new font in effect.

All elements, including text typed into the edit fields, are now using the new the BB Alpha Serif font.

All elements, including text typed into the edit fields, are now using the new the BB Alpha Serif font.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status