Install and use a custom font in a BlackBerry Java application

  1. Import the required classes and interfaces.
    import net.rim.device.api.system.*;
    import net.rim.device.api.ui.*; 
    import net.rim.device.api.ui.container.*;
    import net.rim.device.api.ui.component.*;
    import java.util.*;
  2. Create the application framework by extending the UiApplication class. In main(), create an instance of the new class and invoke enterEventDispatcher() to enable the application to receive events. In the application constructor, invoke pushScreen() to display the custom screen for the application. The FontLoadingDemoScreen class, described in step 3, represents the custom screen.
    public class FontLoadingDemo extends UiApplication
    {
        public static void main(String[] args)
        {
            FontLoadingDemo app = new FontLoadingDemo();
            app.enterEventDispatcher();
        }
        
        public FontLoadingDemo()
        {
            pushScreen(new FontLoadingDemoScreen());
        }
    }
  3. Create the custom screen by extending the MainScreen class. Invoke setTitle() to set the text that appears in the title section of the screen. Create a new LabelField object. You will apply the custom font to this object.
    class FontLoadingDemoScreen extends MainScreen
    {	
        public FontLoadingDemoScreen()
        {
            setTitle("Font Loading Demo");
            LabelField helloWorld = new LabelField("Hello World");
    
        }
    }
  4. In the screen constructor, invoke the FontManager.getInstance() method to get a reference to the FontManager object, then invoke the load() method to install the font. Wrap the load() invocation in an IF statement to check if the installation was successful. The load() method returns a flag specifying if font is successfully installed. The following code specifies that the font that can be used only by the application.
    if (FontManager.getInstance().load("Myfont.ttf", "MyFont", FontManager.APPLICATION_FONT) == FontManager.SUCCESS) 
    {
    }
  5. In the screen constructor, in a try/catch block in the IF statement you created in step 5, create a Font object for the font you just installed. Invoke the setFont() method to apply the font to the LabelField you created in step 5.
    try 
    {
        FontFamily family = FontFamily.forName("MyFont");
        Font myFont = family.getFont(Font.PLAIN, 50);
        helloWorld.setFont(myFont);
    } 
    catch (ClassNotFoundException e) 
    {
        System.out.println(e.getMessage());
    }
  6. In the screen constructor, invoke add() to add the LabelField to the screen.
    add(helloWorld);

Code sample: Installing and using a custom font in a BlackBerry Java application

import net.rim.device.api.system.*;
import net.rim.device.api.ui.*; 
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
import java.util.*;

public class FontLoadingDemo extends UiApplication
{
    public static void main(String[] args)
    {
        FontLoadingDemo app = new FontLoadingDemo();
        app.enterEventDispatcher();
    }
    
    public FontLoadingDemo()
    {
        pushScreen(new FontLoadingDemoScreen());
    }
}

class FontLoadingDemoScreen extends MainScreen
{	
    public FontLoadingDemoScreen()
    {
        setTitle("Font Loading Demo");

        LabelField helloWorld = new LabelField("Hello World");
        
        if (FontManager.getInstance().load("Myfont.ttf", "MyFont", FontManager.APPLICATION_FONT) == FontManager.SUCCESS) 
        {
            try 
            {
                FontFamily typeface = FontFamily.forName("MyFont");
                Font myFont = typeface.getFont(Font.PLAIN, 50);
                helloWorld.setFont(myFont); 
            }
            catch (ClassNotFoundException e) 
            {
                System.out.println(e.getMessage());
            }
        }
        add(helloWorld);
    }
}
Next topic: Spelling checker
Previous topic: Custom fonts

Was this information helpful? Send us your comments.