Help Center

Local Navigation

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
    {
       SampleMenuItem()
       {
          super(20);
       }
    }
  3. Implement the toString() method, to specify text that the menu item displays.
    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 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 the run() method to specify the behavior of the menu item. In the following code sample, when a user clicks the new menu item and a Contact exists, the ContactsDemo application receives the events and invokes ContactsDemo.enterEventDispatcher().
    public Object run(Object context)
    {
       BlackBerryContact c = (BlackBerryContact)context;
       if ( c != null )
       {
          new ContactsDemo().enterEventDispatcher();
       }
       else
       {
          throw new IllegalStateException( "Context is null, expected a Contact instance");
       }
       Dialog.alert("Viewing an email message in the email view");
       return null;
    }

Index


Was this information helpful? Send us your comments.