Add a menu item to a BlackBerry Device Software application

  1. Import the required classes and interfaces.
    import net.rim.blackberry.api.menuitem.*;
    import net.rim.blackberry.api.pdap.*;
    import net.rim.device.api.ui.*;
    import net.rim.device.api.ui.component.*;
  2. Extend the abstract ApplicationMenuItem class to create a menu item. Override the ApplicationMenuItem() constructor with an integer to specify the position of the menu item in the menu. A higher number positions the menu item lower in the menu.
    public class SampleMenuItem extends ApplicationMenuItem
  3. Implement toString() to specify the menu item text.
    public String toString()
       return "Open the Contacts Demo application";
  4. Invoke getInstance() to retrieve the application repository.
    ApplicationMenuItemRepository repository = ApplicationMenuItemRepository.getInstance();
  5. Create an instance of a class to extend the MenuItem class.
    ContactsDemoMenuItem contactsDemoMenuItem = new ContactsDemoMenuItem();
  6. Invoke ApplicationMenuItemRepository.addMenuItem() to add the menu item to the relevant BlackBerry® device application repository.
    repository.addMenuItem(ApplicationMenuItemRepository.MENUITEM_ADDRESSCARD_VIEW, contactsDemoMenuItem);
  7. Implement run() to specify the behavior of the menu item. In the following code sample, when a user clicks the new menu item and a Contact object exists, the ContactsDemo application receives the event and invokes ContactsDemo.enterEventDispatcher().
    public Object run(Object context)
       BlackBerryContact c = (BlackBerryContact)context;
       if ( c != null )
          new ContactsDemo().enterEventDispatcher();
          throw new IllegalStateException( "Context is null, expected a Contact instance");
       Dialog.alert("Viewing an email message in the email view");
       return null;

Was this information helpful? Send us your comments.