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

5 comments:

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

    ReplyDelete
  2. 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.

    ReplyDelete
  3. I have a BlackBerry Curve 8520. I was trying to find a way to change the font from black to a color for texting. Everywhere I look it talks about background color. I know about the themes. The theme colors only color the numbers when dialing. I want to be able to download an application that will allow me to customize the the color of the font. Here is an example of what I am talking about. What I am writing,I want it to be in the color of my choice. How do I go about getting help with finding an application that will do that?

    ReplyDelete
  4. I am trying to find the same as Amy. The only thing I have found is Empower SMS, however, it's not compatible with os 5.0. I too am using a curve 8520. Any idea how to install new fonts and colors? As for the code above, I am new to the java programming world. Can you give a little more detail on how to initiate this code? I have one of the java programs for bb app development, but currently I'm using JCreator for the sake of learning more before I even try jumping in on this. Help?? Please?? :(

    ReplyDelete
  5. HI
    Nice blog i like it
    The latest version of Blackberry MDS v4.0 let the developers wirelessly access web services from Blackberry Smartphones.

    ReplyDelete

Place your comments here...