Date post: | 19-Dec-2015 |
Category: |
Documents |
View: | 223 times |
Download: | 1 times |
Cosc 5/4730
Blackberry and Android:Menus
BLACKBERRY
Menu
• With a MainScreen, you are provided a menu– It has a default Close item, which calls the
onClose() method• If you don’t override it, then it just closes the screen.
– You are provided with 3 methods • addMenuItem(MenuItem item)• removeMenuItem(MenuItem item)• removeAllMenuItems()
Menu (2)• You can add your own menu items, by creating a MenuItem
– It’s runnable to you must have a run() method.• Example:MenuItem getset = new MenuItem("Get Settings", 1,100) {
• 1 is ordinal - Ordering parameter, lower values are placed closer to the top of the menu screen
• 100 is the priority of the menu item. A lower value indicates a higher priority
public void run() { gettingsettings(); } };addMenuItem(getset); //this method is from the MainScreen
makeMenu method
• The second method to add, change, customize the menu is override the MakeMenu method.
protected void makeMenu(Menu menu, int instance) { super.makeMenu(menu,instance); //m1 is MenuItem variable menu.add(MenuItem m1); menu.addSeparator(); …
}
makeMenu method (2)
• In API 5.0.0+– You can customize the background, border and
font of the menu• Using Menu.SetBackground, Menu.setborder, and
menu.SetFont
– You can also add menu icons, using the MenuItem.setIcon method.• Don’t need to override makeMenu to add and icon
– getset.setIcon(Image menuIcon);
Submenus and popup menus• are available in API 6.0.0+
– In the net.rim.device.api.ui.menu package.
• Create a “submenu”, – then add it to the menu.
protected void makeMenu( Menu menu, int instance ) { SubMenu statusSubMenu = new SubMenu(null,"My Status",300,3); statusSubMenu.add(_status1); statusSubMenu.add(_status2); menu.add(statusSubMenu); super.makeMenu(menu, instance); };
popup menus
• You can also create context popup menus
• Except I can’t get theExample code to show aPopup menu.
ToolBars
• Toolbars provide users with a quick and easy way to access frequent actions for an application or screen. Each toolbar consists of a set of icons that appears along the bottom of the screen. – API 6.0.0+
– Convention says the icons should be no more then 33x33– But example shown is obviously using much wider icons.
Toolbar example
• Simple some code (see the example on hand outs)
• Create a ToolbarManagerToolbarManager manager = new ToolbarManager();setToolbar(manager);
• Create ToobarButtonFieldToolbarButtonField button1 = new ToolbarButtonField(myImage, new StringProvider("butn1"));• Add commands to button (code skipped)
• Add the ToobarButtonField to the manager
manager.add(button1);
ANDROID
Menu
• By default, every Activity supports an options menu of actions or options. You can add items to this menu and handle clicks on your additions
• The easiest way to add menu items is override onCreateOptionsMenu(Menu menu) and onOptionsItemSelected(MenuItem)
onCreateOptionsMenu
• create IDs for the menu items, need them later to find out which menu was selected.
protected static final int Menu1_ID = Menu.FIRST; protected static final int Menu2_ID = Menu.FIRST+1;• Override and add the menu items you want.@Override public boolean onCreateOptionsMenu(Menu menu) {• add(int groupId, int itemId, int order, CharSequence)
menu.add(0, Menu1_ID, 0, "Menu 1");menu.add(0, Menu2_ID, 0, "Menu 2");return super.onCreateOptionsMenu(menu);
}
onCreateOptionsMenu (2)
• You can also add sub menu as well– addSubMenu
• performShortcut(int keyCode, KeyEvent event, int flags)– Execute the menu item action associated with the given shortcut
character.• removeGroup(int groupId)
– Remove all items in the given group.• removeItem(int id)
– Remove the item with the given identifier.• clear()
– Remove all existing items from the menu, leaving it empty as if it had just been created.
onOptionsItemSelected@Override public boolean onOptionsItemSelected(MenuItem item) {switch (item.getItemId()) {
case Menu1_ID://do somethingreturn true; //we processed the menu item
case Menu2_ID://do somethingreturn true;
default://super does something.return super.onOptionsItemSelected(item);
}}
Menu Example
• You can add 5 menu items and the they will stack. With 6 or more menu items, you will get a MORE menu item
• So put the important menu items as the first ones and the least important (used) farther down.
JellyBean and menus
• Starting in ICS (v3), you can use a xml layout– Also create context or popup menus• A note they are differences between v3 and v4. I’m
ignoring v3 and using v4.
– First create a menu xml (normally in res.menu) with menu as the type.• You can add items (and sub menus). You can also group
the items as well.
Xml example: <group android:id="@+id/group1"> <item android:id="@+id/item1" android:orderInCategory="5" android:title="item1"/> <item android:id="@+id/item2" android:orderInCategory="10" android:title="item2"/> <item android:id="@+id/item3" android:orderInCategory="1" android:title="item3"/> <item android:id="@+id/item4" android:orderInCategory="3" android:title="item4"/> <item android:id="@+id/item5" android:orderInCategory="2" android:title="item5"/> </group>
• Note the orderInCategory determines the order of display, so this will show:
Item3Item5Item3Item1item2
Java code
• This is all that is needed for onCreateOpensMenu– No constants are needed either.@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.menuv4, menu);return true;}
onOptionsItemSelected
• Use the R.id.X instead of constants.switch (item.getItemId()) { case R.id.item1:
//do somethingreturn true;
…
Popup menus.
• Add a click listener (or longtouch, whatever) to anything.– We are using a TextView, so make sure it clickable– It will then call our code, called
showPopupMenu(View v) • Note this is not an override, just a method we are using
public void onClick(View v) {showPopupMenu(v);
}
showPopupMenuprivate void showPopupMenu(View v){
PopupMenu popupM = new PopupMenu(this, v);popupM.inflate(R.menu.popup);
popupM.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override public boolean onMenuItemClick(MenuItem item) {//do something return true; }});
popupM.show(); }
Example
• Using the menu
• Using the popup menu
code
• The code for these examples is on the web pages
• Blackberry: menu Demo.zip• Android: menuV2.zip and menuV4.zip
QA&