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 – 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 – 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 the Example code to show a Popup 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 – 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 ToolbarManager ToolbarManager manager = new ToolbarManager(); setToolbar(manager); Create ToobarButtonField ToolbarButtonField 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 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.
public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case Menu1_ID: //do something return true; //we processed the menu item case Menu2_ID: //do something return 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: <item android:orderInCategory="5" android:title="item1"/> <item android:orderInCategory="10" android:title="item2"/> <item android:orderInCategory="1" android:title="item3"/> <item android:orderInCategory="3" android:title="item4"/> <item android:orderInCategory="2" android:title="item5"/> Note the orderInCategory determines the order of display, so this will show: Item3 Item5 Item3 Item1 item2
Java code This is all that is needed for onCreateOpensMenu – No constants are needed public 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 something return 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); }
showPopupMenu private void showPopupMenu(View v){ PopupMenu popupM = new PopupMenu(this, v); popupM.inflate(R.menu.popup); popupM.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() 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
Q A &