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:
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 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.
BLACKBERRY Related Interview Questions
|J2ME Interview Questions||BLACKBERRY Interview Questions|
|Android Interview Questions||Retail Interview Questions|
|Telecommunication Project Management Interview Questions||Mobile Testing Interview Questions|
|Telemarketing Interview Questions||Mobile Application Testing Interview Questions|
|Mobile Marketing Interview Questions||Telecom Billing Interview Questions|
|Mobile Application Architect Interview Questions|
Setting The Stage
What Makes A Blackberry Application?
User Interface Basics
Beyond The Basics Of User Interfaces
Hello Out There! Making A Network-enabled Application
Where Am I? Using Location-based Services
Getting Your App Out There: Packaging And Publishing
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.