Cosc 5/4730 Blackberry and Android: Menus. BLACKBERRY

  • View
    215

  • Download
    1

Embed Size (px)

Text of Cosc 5/4730 Blackberry and Android: Menus. BLACKBERRY

  • Slide 1
  • Cosc 5/4730 Blackberry and Android: Menus
  • Slide 2
  • BLACKBERRY
  • Slide 3
  • Menu With a MainScreen, you are provided a menu It has a default Close item, which calls the onClose() method If you dont override it, then it just closes the screen. You are provided with 3 methods addMenuItem(MenuItem item) removeMenuItem(MenuItem item) removeAllMenuItems()
  • Slide 4
  • Menu (2) You can add your own menu items, by creating a MenuItem Its 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
  • Slide 5
  • 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(); }
  • Slide 6
  • 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. Dont need to override makeMenu to add and icon getset.setIcon(Image menuIcon);
  • Slide 7
  • 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); };
  • Slide 8
  • popup menus You can also create context popup menus Except I cant get the Example code to show a Popup menu.
  • Slide 9
  • 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.
  • Slide 10
  • 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);
  • Slide 11
  • ANDROID
  • Slide 12
  • 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)
  • Slide 13
  • 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); }
  • Slide 14
  • 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.
  • Slide 15
  • onOptionsItemSelected @Override 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); }
  • Slide 16
  • 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.
  • Slide 17
  • 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. Im 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.
  • Slide 18 Note the orderInCategory determines the order of display, so this will show: Item3 Item5 Item3 Item1 item2">
  • Xml example: Note the orderInCategory determines the order of display, so this will show: Item3 Item5 Item3 Item1 item2
  • Slide 19
  • Java code This is all that is needed for onCreateOpensMenu No constants are needed either. @Override 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; }
  • Slide 20
  • onOptionsItemSelected Use the R.id.X instead of constants. switch (item.getItemId()) { case R.id.item1: //do something return true;
  • Slide 21
  • 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); }
  • Slide 22
  • showPopupMenu private 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(); }
  • Slide 23
  • Example Using the menu Using the popup menu
  • Slide 24
  • code The code for these examples is on the web pages Blackberry: menu Demo.zip Android: menuV2.zip and menuV4.zip
  • Slide 25
  • Q A &