Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Cosc 5/4730 Blackberry and Android: Menus. BLACKBERRY."— Presentation transcript:

1 Cosc 5/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(MenuItem item) 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: 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

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 ) { SubMenu statusSubMenu = 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 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); }

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.

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

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: <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"/> 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 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; }

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

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 &


Download ppt "Cosc 5/4730 Blackberry and Android: Menus. BLACKBERRY."

Similar presentations


Ads by Google