Wednesday, April 8, 2009

How To : Change fonts in a BlackBerry application

How To The BlackBerry device supports multiple fonts in various sizes. These fonts can be used in custom BlackBerry applications and can be changed during runtime. The number of fonts available on a device depends on the model and the BlackBerry Device Software version installed.

The following example illustrates how to use fonts in a custom BlackBerry application and change the font during runtime:

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
public final class FontChange extends UiApplication
{
    private Font font;
    //Get all fonts available on the BlackBerry
    private FontFamily fontFamily[] = FontFamily.getFontFamilies();
    public static void main(String[] args)
    {
            FontChange theApp = new FontChange();
            theApp.enterEventDispatcher();
    }
    public FontChange()
    {
        MainScreen mainScreen;
        FontChangeListener fontChangeListener = new FontChangeListener();
        //Set the current font to the first in the font list.
        font = fontFamily[0].getFont(FontFamily.SCALABLE_FONT, 10);
        Font.setDefaultFont(font);
        mainScreen = new MainScreen();
        //Set the title of the mainscreen.
        LabelField title = new LabelField("Font Test Example",
            LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
        mainScreen.setTitle(title);
        ObjectChoiceField pickFont = new ObjectChoiceField("Select a font:",
            fontFamily, 0);
        pickFont.setChangeListener(null);
        pickFont.setChangeListener(fontChangeListener);
        mainScreen.add(pickFont);
        mainScreen.add(new SeparatorField());
        //RichTextField that displays text in the specified colour.
        RichTextField fontText = new RichTextField
            ("The quick red fox jumps over the lazy brown dog.")
        {
            public void paint(Graphics graphics)
            {
                //Change the colour of the text in the RichTextField to green.
                graphics.setColor(0x00008800);
                super.paint(graphics);
            }
        };
        mainScreen.add(fontText);
        //Display the main screen
        pushScreen(mainScreen);
    }
    //Create a custom FieldChangeListener that will change the current font
    //to what is selected in the ObjectChangeField.
    class FontChangeListener implements FieldChangeListener
    {
        public void fieldChanged(Field field, int context)
        {
            //If the ObjectCoiceField has changed
            if (field instanceof ObjectChoiceField)
            {
                //Get the new font selected in the ObjectChoiceField and set the
                //current font to match what is selected.
                ObjectChoiceField choiceField = (ObjectChoiceField)field;
                font = fontFamily[choiceField.getSelectedIndex()].getFont
                    (FontFamily.SCALABLE_FONT,10);
                Font.setDefaultFont(font);
            }
        }
    }
}

2 comments:

stephanie said...

Tq for your info :)
Can u give me some example about change the font colour?

R. K. Saytode said...

Hello Stephanie,
As you can see in above example :
public void paint(Graphics graphics)
{
//Change the colour of the text in the
//RichTextField to green.
graphics.setColor(0x00008800);
super.paint(graphics);
}

----
paint() method is used to change the font color. You can change font color for any field by the same as above.

Post a Comment

Place your comments here...