Вы находитесь на странице: 1из 15

Android - Menus

- Android Apps Development Team

Lets Revise
1. What are the different types of Resources? Layout, drawable, animation, string, colors etc. 2. What is application internationalization? Application support for multiple languages 3. How the Layout resources supported for different orientations? Maintains separate layout folders, landscape and portrait modes

Agenda
1. Menus Basics 1. Options Menu 2. Context Menu 3. Submenus 2. Other Menu Features 3. Q & A

1. Menus Basics
What is a Menu? It is a mechanism to expose the applications functionality without consuming the view space Types of Menus
Options Menu: The primary menu for an Activity, which appears when the user presses the device MENU key (Menu Key on hardware device), it contains icon menu and expanded menu Context Menu: A floating list of menu items that appears when the user performs a long-press on a View Sub-menu: A floating list of menu items that the user opens by pressing a menu item in the Options Menu or a context menu

Menus Basics Options Menu


Options Menu Basic Application functions can be added under this menu The user can open the Options Menu with the device MENU key the first visible portion of the Options Menu is called a Menu Menu will hold the first six menu items. If added more than six items to the Options Menu, Android places the sixth item and those after it into the Expanded Menu, which the user can open with the "More" menu item

Menus Basics Options Menu


Defining and Inflating Menus Define menu and its items in the XML menu resource file Inflate the menu resource from the applications source code Create XML file in res/menu/ directory
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/new_game" android:icon="@drawable/ic_new_game" android:title="@string/new_game" /> <item android:id="@+id/quit" android:icon="@drawable/ic_quit" android:title="@string/quit" /> </menu>

Menus Basics Options Menu


Inflating the menu resource from the Activity,

@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.game_menu, menu); return true; }

Menus Basics Context menu


A context menu is conceptually similar to the menu displayed when the user performs a "right-click" on a PC On Android, a context menu is displayed when the user performs a "long press" (press and hold) on an item Application a can create a context menu for any View In order for a View to provide a context menu, you must "register" the view for a context menu Call registerForContextMenu() and pass it the View we want to give a context menu. When this View then receives a long-press, it displays a context menu To define the context menu's appearance and behavior, override your Activity's context menu callback methods, onCreateContextMenu() and onContextItemSelected()

Menus Basics Context Menu


For example, here's an onCreateContextMenu() that uses the context_menu.xml menu resource:
@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.context_menu, menu); }

Handling the selected item using onContextItemSelected()


@Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); switch (item.getItemId()) { case R.id.edit: editNote(info.id); return true; case R.id.delete: deleteNote(info.id); return true; default: return super.onContextItemSelected(item); } }

Menus Basics - Submenus


A submenu is a menu that the user can open by selecting an item in another menu. You can add a submenu to any menu, except another submenu When creating your menu resource, you can create a submenu by adding a <menu> element as the child of an <item>. Android does not support nested submenus
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/file" android:icon="@drawable/file" android:title="@string/file" > <!-- "file" submenu --> <menu"> <item android:id="@+id/new" android:title="@string/new" /> <item android:id="@+id/open" android:title="@string/open" /> </menu> </item> </menu>

Menus Other Menu Features


Menu groups
A menu group is a collection of menu items that has similar nature Show or hide all items Enable or disable all items Specify whether items are checkable You can create a group by nesting <item> elements inside a <group> element in your menu resource
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/item1" android:icon="@drawable/item1" android:title="@string/item1" /> <!-- menu group --> <group android:id="@+id/group1"> <item android:id="@+id/groupItem1" android:title="@string/groupItem1" /> <item android:id="@+id/groupItem2" android:title="@string/groupItem2" /> </group> </menu>

Menus Other Menu Features


Checkable Menu Items Used as an option to turn on or off Options are check buttons, radio buttons Uses android:checkableBehaviorattribute in the <group> elements, This attribute accepts, Single (Radio buttons) or All (Check boxes) Example:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single"> <item android:id="@+id/red" android:title="@string/red" /> <item android:id="@+id/blue" android:title="@string/blue" /> </group> </menu>

Menus - Other Menu Features


Shortcut Keys
To have a quick-access, shortcut keys are used using letters and/or numbers to menu items Attributes used along with the <item> element are android:alphabeticShortcut andandroid:numericShortcut attributes Shortcut keys for menu items only work on devices with a hardware keyboard. Shortcuts cannot be added to items in a Context Menu.

Menus Practical Example


Practical example on Eclipse

Questions

Вам также может понравиться