1 / 25

Cosc 5/4730

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

neron
Télécharger la présentation

Cosc 5/4730

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Cosc5/4730 Blackberry and Android: Menus

  2. Blackberry

  3. 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(MenuItemitem) • removeMenuItem(MenuItem item) • removeAllMenuItems()

  4. Menu (2) • You can add your own menu items, by creating a MenuItem • It’s runnable to you must have a run() method. • Example: MenuItemgetset = 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

  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(); … }

  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. • Don’t need to override makeMenu to add and icon • getset.setIcon(Image menuIcon);

  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 ) { SubMenustatusSubMenu = new SubMenu(null,"My Status",300,3); statusSubMenu.add(_status1); statusSubMenu.add(_status2); menu.add(statusSubMenu); super.makeMenu(menu, instance); };

  8. popup menus • You can also create context popup menus • Except I can’t get the Example code to show a Popup menu.

  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.

  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);

  11. Android

  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)

  13. onCreateOptionsMenu • create IDs for the menu items, need them later to find out which menu was selected. protected static final intMenu1_ID = Menu.FIRST; protected static final intMenu2_ID = Menu.FIRST+1; • Override and add the menu items you want. @Override public booleanonCreateOptionsMenu(Menu menu) { • add(intgroupId, intitemId, int order, CharSequence) menu.add(0, Menu1_ID, 0, "Menu 1"); menu.add(0, Menu2_ID, 0, "Menu 2"); return super.onCreateOptionsMenu(menu); }

  14. onCreateOptionsMenu (2) • You can also add sub menu as well • addSubMenu • performShortcut(intkeyCode, KeyEvent event, int flags) • Execute the menu item action associated with the given shortcut character. • removeGroup(intgroupId) • Remove all items in the given group. • removeItem(intid) • 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.

  15. onOptionsItemSelected @Override public booleanonOptionsItemSelected(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); } }

  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.

  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. 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.

  18. 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: Item3 Item5 Item3 Item1 item2

  19. Java code • This is all that is needed for onCreateOpensMenu • No constants are needed either. @Override public booleanonCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menuv4, menu); return true; }

  20. onOptionsItemSelected • Use the R.id.X instead of constants. switch (item.getItemId()) { case R.id.item1: //do something return true; …

  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); }

  22. showPopupMenu private void showPopupMenu(View v){ PopupMenupopupM = new PopupMenu(this, v); popupM.inflate(R.menu.popup); popupM.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public booleanonMenuItemClick(MenuItem item) { //do something return true; } }); popupM.show(); }

  23. Example • Using the menu • Using the popup menu

  24. code • The code for these examples is on the web pages • Blackberry: menu Demo.zip • Android: menuV2.zip and menuV4.zip

  25. Q A &

More Related