Change the appearance of a menu

  1. Import the required classes and interfaces.
    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.ui.decor.*;
    import net.rim.device.api.system.*;
  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 CreateCustomMenuScreen class , which is described in step 3, represents the custom screen.
    public class CreateCustomMenu extends UiApplication 
    {
       public static void main(String[] args)
       {
          CreateCustomMenu theApp = new CreateCustomMenu();
          theApp.enterEventDispatcher();
       }
       public CreateCustomMenu()
       {
          pushScreen(new CreateCustomMenuScreen());
       }
    }
  3. Create the custom screen for the application by extending the MainScreen class. In the screen constructor, invoke setTitle() to specify the title for the screen. Invoke add() to add a text field on the screen.
    class CreateCustomMenuScreen extends MainScreen
    {
        Background _menuBackground;
        Border _menuBorder;
        Font _menuFont;
        CreateCustomMenuScreen()
        {
            setTitle("Custom Menu Sample");
            add(new RichTextField("Creating a custom menu"));
        }
    }
  4. In the screen constructor, specify the appearance of the menu. Create the spacing for the border around the menu by creating an XYEdges object. Invoke createRoundedBorder() to create a border with rounded corners. Invoke createSolidTransparentBackground() to create a transparent background color for the menu.
    XYEdges thickPadding = new XYEdges(10, 10, 10, 10);
    _menuBorder = BorderFactory.createRoundedBorder(thickPadding, Border.STYLE_DOTTED);
    _menuBackground = BackgroundFactory.createSolidTransparentBackground(Color.LIGHTSTEELBLUE, 50);
  5. In the screen constructor, specify the font for the menu by using a FontFamily object. Invoke forName() to retrieve a font from on the BlackBerry® device. Invoke getFont() to specify the style and size of the font.
    try
    {
        FontFamily family = FontFamily.forName("BBCasual");
        _menuFont = family.getFont(Font.PLAIN, 30, Ui.UNITS_px);
    }
    catch(final ClassNotFoundException cnfe)
    {
        UiApplication.getUiApplication().invokeLater(new Runnable()
        {
            public void run()
            {
                Dialog.alert("FontFamily.forName() threw " + cnfe.toString());
            }
        });      
    }
  6. In the screen class, override makeMenu() to apply the appearance of the menu. Invoke setBackground(), setBorder(), and setFont() to apply the appearance the menu that you specified in steps 4 and 5.
    protected void makeMenu(Menu menu, int context)
    {
        menu.setBorder(_menuBorder);
        menu.setBackground(_menuBackground);
        menu.setFont(_menuFont);
        super.makeMenu(menu, context);
    }

Code sample: Changing the appearance of a menu

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.ui.decor.*;
import net.rim.device.api.system.*;

public class CreateCustomMenu extends UiApplication
{
   public static void main(String[] args)
   {
      CreateCustomMenu theApp = new CreateCustomMenu();
      theApp.enterEventDispatcher();
   }
   public CreateCustomMenu()
   {
      pushScreen(new CreateCustomMenuScreen());
   }
} 
class CreateCustomMenuScreen extends MainScreen
{
    Border _menuBorder;
    Background _menuBackground;
    Font _menuFont;
    
    CreateCustomMenuScreen()
    {
        setTitle("Custom Menu Sample");
        add(new RichTextField("Creating a custom menu"));
         
        XYEdges thickPadding = new XYEdges(10, 10, 10, 10);
        _menuBorder = BorderFactory.createRoundedBorder(thickPadding, Border.STYLE_DOTTED);        
        _menuBackground = BackgroundFactory.createSolidTransparentBackground(Color.LIGHTSTEELBLUE, 50);
        try
        {
            FontFamily family = FontFamily.forName("BBCasual");
            _menuFont = family.getFont(Font.PLAIN, 30, Ui.UNITS_px);
        }
        catch(final ClassNotFoundException cnfe)
        {
            UiApplication.getUiApplication().invokeLater(new Runnable()
            {
                public void run()
                {
                    Dialog.alert("FontFamily.forName() threw " + cnfe.toString());
                }
            });              
        }            
    }
    protected void makeMenu(Menu menu, int context)
    {
        menu.setBorder(_menuBorder);
        menu.setBackground(_menuBackground);
        menu.setFont(_menuFont);
        super.makeMenu(menu, context);
    }        
}
Next topic: Custom fonts

Was this information helpful? Send us your comments.