If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Change fonts in a BlackBerry application

by BlackBerry Development Advisor ‎02-12-2010 03:22 PM - edited ‎09-16-2010 03:46 PM (8,114 Views)

Summary

 

This article applies to the following:

  • BlackBerry® Java™ Development Environment (JDE)
  • BlackBerry wireless devices based on Java

Procedure

 

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:

 

 

/**
 * Font change sample
 *
 */

package com.samples.fontChange;

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);
            }
        }
    }
}

 

Contributors