Creating Menus BLACKBERRY

Menus are everywhere in a BlackBerry application; in fact, you should try to provide menu options for most major functionality, because the menu is easier to access on most BlackBerry devices than buttons on the screen.The screen handles displaying menus, and menu items contain their own logic for performing actions.

Understanding Menu Items

Individual items in a menu are instances of net.rim.device.api.ui.MenuItem. This class is abstract, and implements Runnable (it has a run method that you need to implement). The run method is executed on the event thread when the user clicks the menu item, so you can safely modify the UI from within the run method. This also means you shouldn’t do any heavy processing or networking in the run method without starting a separate thread. Each menu item has three pieces of information associated with it: text to display, a priority, and an ordinal. The priority determines which menu item is initially highlighted when the menu is first displayed; the item with the lowest value for priority will be highlighted when the menu is first opened. The ordinal determines where the item will appear in the menu relative to other menu items. A menu item will appear above menu items with higher ordinal values and below menu items with lower ordinal values. Two menu items with the same ordinal will appear in the order they were added to the menu, top to bottom.

Implementing the Login and Clear Menu Items

We’ll implement two menu items for our application corresponding to the Login and Clear actions. We’ll create a new class for each menu item and declare these as inner classes within UiFunMainScreen—because we’ll only use them here and to give them access to UiFunMainScreen’s private login and clearTextFields methods:

Notice how we arranged the ordinal and priorities with the menu items. ClearMenuItem has a lower ordinal value and higher priority value than LoginMenuItem. Therefore, Clear will appear above Login on the menu, but Login will be highlighted by default This arrangement lets us mirror the order of the buttons on screen, but make the most likely user choice the default one, saving our users a bit of time.

There are a couple of places we can add menu items.One is in the screen’s constructor, by calling getMenu and adding items to the Menu object we get back. The other is by overriding makeMenu in our screen class. We’ll do the latter, because makeMenu is useful when creating context sensitive menus (menus whose items may change depending on the state of the screen). The makeMenu method in UiFunMainScreen should look like this:

Supporting Different Menu Instances

The instance parameter is used to identify which menu we’re supposed to show. BlackBerry applications display a few slightly different menus depending on how the menu is displayed and the context of the screen and controls.The defined instances follow:

  • Menu.INSTANCE_CONTEXT: The menu was displayed by clicking the trackball. Usually, this menu will be a subset of only the items available in the default menu that are applicable to the currently focused control or section of the screen. For example, Close wouldn’t show up here. The BlackBerry automatically adds a Full Menu item to this menu, which causes makeMenu to be called with INSTANCE_DEFAULT as the instance parameter.
  • Menu.INSTANCE_CONTEXT_SELECTION: This is the same as INSTANCE_CONTEXT, but the menu is displayed while the user has some text selected. You can use this to display items that may only apply when you can copy text.
  • Menu.INSTANCE_DEFAULT: In this case, menu is displayed by pressing the menu key. This menu should contain all the items in the context menu, in addition to any items that apply to the application as a whole. Close would show up here.

In our application, we want Login and Clear to show up no matter which control has focus, since everything is related to logging in. Effectively, we can ignore the instance parameter in makeMenu, as the BlackBerry will automatically take care of putting the Close item only in the appropriate menu instance. This is why our makeMenu method is so simple.With that discussion out of the way, we can now run the application and see our menu items, which should look like the ones in Figures.

In the context menu, notice the Login is selected, and the Full Menu item is present.

In the context menu, notice the Login is selected, and the Full Menu item is present.

In the default menu, notice the Login is selected, and many more menu items are present.

In the default menu, notice the Login is selected, and many more menu items are present.

You’ll notice that the instance menu can only show up when the currently focused control does not consume the click. Our ButtonFields have the ButtonField.CONSUME_ CLICK style flag set, so the instance menu won’t show up when you click the trackball while they’re focused, which is how you’d expect buttons to behave. The CheckboxField and Object Choice Field also consume the trackball click, so we’ll see the context menu only when we click the trackball on one of the text fields. Clicking either the Clear or Login menu item will have the same effect as clicking the Clear or Login button.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

BLACKBERRY Topics