Create and register a menu item

Extend the ApplicationMenuItem class to define a menu item to display in BlackBerry® Device Software applications.

  1. Import the required classes and interfaces.
    import java.lang.IllegalStateException;
    import net.rim.blackberry.api.menuitem.ApplicationMenuItem;
    import net.rim.blackberry.api.menuitem.ApplicationMenuItemRepository;
    
  2. To create and register a menu item, perform the following tasks:

    Task

    Steps

    Define a menu item

    1. Extend the ApplicationMenuItem class.
      public class SampleMenuItem extends ApplicationMenuItem { ... }

    Specify the position of the menu item in the menu

    1. Construct the ApplicationMenuItem. A higher number in the constructor means that the menu item appears lower in the menu.
      SampleMenuItem() 
      {
        super(0x350100);
      }
      

    Specify the menu item text

    1. Implement toString().
      public String toString() 
      {
        return "My menu item";
      }
      

    Specify the behavior of the menu item

    1. Implement run().
      public Object run(Object context) 
      {
        // the menu's action here
        return null;
      }
      

    Register the menu item

    1. Invoke ApplicationMenuItemRepository.addMenuItem().
      ApplicationMenuItemRepository amir = 
        ApplicationMenuItemRepository.getInstance();
      amir.addMenuItem(
        ApplicationMenuItemRepository.MENUITEM_PHONE, 
        mySampleMenuItem);
      

Code sample: Creating and registering a menu item

// Create menu item 
int placement = 0x350100; 
ApplicationMenuItem ami = new ApplicationMenuItem(placement) 
{ 
   public Object run(Object context) 
   { 
      // do something 
      return null; 
   } 

   public String toString()
   { 
      return "My menu item"; 
   } 
}; 

// Register menu item to display when user views a contact
ApplicationMenuItemRepository amir = ApplicationMenuItemRepository.getInstance();
amir.addMenuItem(ApplicationMenuItemRepository.MENUITEM_ADDRESSCARD_VIEW, ami); 

For more information, see the Application Integration category overview in the API reference for the BlackBerry® Java® Development Environment.


Was this information helpful? Send us your comments.