Menus are a common user interface component in many types of applications. To provide a familiar and consistent user experience, you should use the Show
Beginning with Android 3.0 (API level 11), Android-powered devices are no longer required to provide a dedicated Menu button. With this change, Android apps should migrate away from a dependence on the traditional 6-item menu panel and instead provide an app bar to present common user actions. Although the design and user experience for some menu items have changed, the semantics to define a set of actions and options is still based on the See the section about Creating an Options Menu. Context menu and contextual action modeA context menu is a floating menu that appears when the user performs a long-click on an element. It provides actions that affect the selected content or context frame.The contextual action mode displays action items that affect the selected content in a bar at the top of the screen and allows the user to select multiple items. See the section about Creating Contextual Menus. Popup menuA popup menu displays a list of items in a vertical list that's anchored to the view that invoked the menu. It's good for providing an overflow of actions that relate to specific content or to provide options for a second part of a command. Actions in a popup menu should not directly affect the corresponding content—that's what contextual actions are for. Rather, the popup menu is for extended actions that relate to regions of content in your activity.See the section about Creating a Popup Menu. Defining a Menu in XMLFor all menu types, Android provides a standard XML format to define menu items. Instead of building a menu in your activity's code, you should define a menu and all its items in an XML menu resource. You can then inflate the menu resource (load it as a Using a menu resource is a good practice for a few reasons:
To define the menu, create an XML file inside your project's <menu> Defines a Menu , which is a container for menu items. A <menu> element must be the root node for the file and can hold one or more <item> and <group> elements.<item> Creates a MenuItem , which represents a single item in a menu. This element may contain a nested <menu> element in order to create a submenu.<group> An optional, invisible container for <item> elements. It allows you to
categorize menu items so they share properties such as active state and visibility. For more information, see the section about Creating Menu Groups.Here's an example menu named <?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" android:showAsAction="ifRoom"/> <item android:id="@+id/help" android:icon="@drawable/ic_help" android:title="@string/help" /> </menu> The android:id
A resource ID that's unique to the item, which allows the application to recognize the item when the user selects it.android:icon A reference to a drawable to use as the item's icon.android:title A reference to a string to use as the item's title.android:showAsAction Specifies when and how this item should appear as an action item in the app bar.These are the most important attributes you should use, but there are many more available. For information about all the supported attributes, see the Menu Resource document. You can add a submenu to an item in any menu by adding a <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/file" android:title="@string/file" > <!-- "file" submenu --> <menu> <item android:id="@+id/create_new" android:title="@string/create_new" /> <item android:id="@+id/open" android:title="@string/open" /> </menu> </item> </menu> To use the menu in
your activity, you need to inflate the menu resource (convert the XML resource into a programmable object) using Figure 1. Options menu in the Browser. The options menu is where you should include actions and other options that are relevant to the current activity context, such as "Search," "Compose email," and "Settings." Where the items in your options menu appear on the screen depends on the version for which you've developed your application:
Figure 2. The Google Sheets app, showing several buttons, including the action overflow button. You can declare items for the options menu from either your To specify the options menu for an activity, override
You can also add menu items using If you've
developed your application for Android 2.3.x and lower, the system calls When the user selects an item from the options menu (including action items in the app bar), the system calls your activity's
When you successfully handle a menu item, return If your activity includes fragments, the system first calls Tip: Android 3.0 adds the ability for you to define the on-click behavior for a menu item in XML, using the Tip: If your application contains multiple activities and some of
them provide the same options menu, consider creating an activity that implements nothing except the After the system calls If you want to modify the options menu based on events that occur during the activity
lifecycle, you can do so in the On Android 2.3.x and lower, the system calls On Android 3.0 and higher, the options menu is considered to always be open when menu items are presented in the app bar. When an
event occurs and you want to perform a menu update, you must call Note: You should never change items in the options menu based on the Figure 3. Screenshots of a floating context menu (left) and the contextual action bar (right). A
contextual menu offers actions that affect a specific item or context frame in the UI. You can provide a context menu for any view, but they are most often used for items in a There are two ways to provide contextual actions:
Note: The contextual action mode is available on Android 3.0 (API level 11) and higher and is the preferred technique for displaying contextual actions when available. If your app supports versions lower than 3.0 then you should fall back to a floating context menu on those devices. To provide a floating context menu:
Using the contextual action modeThe contextual action mode is a system implementation of Note: The contextual action bar is not necessarily associated with the app bar. They operate independently, even though the contextual action bar visually overtakes the app bar position. For views that provide contextual actions, you should usually invoke the contextual action mode upon one of two events (or both):
How your application invokes the contextual action mode and defines the behavior for each action depends on your design. There are basically two designs:
The following sections describe the setup required for each scenario. Enabling the contextual action mode for individual viewsIf you want to invoke the contextual action mode only when the user selects specific views, you should:
For example:
Enabling batch contextual actions in a ListView or GridViewIf you have a collection of items in a
For example:
That's it. Now when the user selects an item with a long-click, the system calls the In some cases in which the contextual actions provide common action items, you might want to add a checkbox or a similar UI element that allows users to select items, because they might not discover the long-click behavior. When a user selects the checkbox, you can invoke the contextual action mode by setting the respective list item to the checked state with Figure 4. A popup menu in the Gmail app, anchored to the overflow button at the top-right. A
Note: If you define your menu in XML, here's how you can show the popup menu:
For example, here's a button with the <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_overflow_holo_dark" android:contentDescription="@string/descr_overflow_button" android:onClick="showPopup" /> The activity can then show the popup menu like this:
In API level 14 and higher, you can combine the two lines that inflate the menu with The menu is dismissed when the user selects an item or touches outside the menu area. You can listen for the dismiss event using To perform an action when the user selects a menu item, you must implement the For example:
Creating Menu GroupsA menu group is a collection of menu items that share certain traits. With a group, you can:
You can create a group by nesting Here's an example menu resource that includes a group: <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_save" android:icon="@drawable/menu_save" android:title="@string/menu_save" /> <!-- menu group --> <group android:id="@+id/group_delete"> <item android:id="@+id/menu_archive" android:title="@string/menu_archive" /> <item android:id="@+id/menu_delete" android:title="@string/menu_delete" /> </group> </menu> The items that are in the group appear at the same level as the first item—all three items in the menu are siblings. However, you can modify the traits of the two items in the group by referencing the group ID and
using the methods listed above. The system will also never separate grouped items. For example, if you declare Using checkable menu itemsFigure 5. Screenshot of a submenu with checkable items. A menu can be useful as an interface for turning options on and off, using a checkbox for stand-alone options, or radio buttons for groups of mutually exclusive options. Figure 5 shows a submenu with items that are checkable with radio buttons. Note: Menu items in the Icon Menu (from the options menu) cannot display a checkbox or radio button. If you choose to make items in the Icon Menu checkable, you must manually indicate the checked state by swapping the icon and/or text each time the state changes. You can define the checkable behavior for individual menu items using the <?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> The single Only one item from the group can be checked (radio buttons)all All items can be
checked (checkboxes)none No items are checkableYou can apply a default checked state to an item using the When a checkable item is selected, the system calls your respective item-selected callback method (such as
If you don't set the checked state this way, then the visible state of the item (the checkbox or radio button) will not change when the user selects it. When you do set the state, the activity preserves the checked state of the item so that when the user opens the menu later, the checked state that you set is visible. Note: Checkable menu items are intended to be used only on a per-session basis and not saved after the application is destroyed. If you have application settings that you would like to save for the user, you should store the data using Shared Preferences. Adding Menu Items Based on an IntentSometimes you'll want a menu item to launch an activity using an However, if you are not certain that the user's device contains an application that handles the intent, then adding a menu item that invokes it can result in a non-functioning menu item, because the intent might not resolve to an activity. To solve this, Android lets you dynamically add menu items to your menu when Android finds activities on the device that handle your intent. To add menu items based on available activities that accept an intent:
If there are no applications installed that satisfy the intent, then no menu items are added. Note: For example:
For each activity found that provides an intent filter matching the intent defined, a menu item is added, using the value in the intent filter's Note: When you call Allowing your activity to be added to other menusYou can also offer the services of your activity to other applications, so your application can be included in the menu of others (reverse the roles described above). To be included in other application menus, you need to define an intent filter as usual, but be sure to include the <intent-filter label="@string/resize_image"> ... <category android:name="android.intent.category.ALTERNATIVE" /> <category android:name="android.intent.category.SELECTED_ALTERNATIVE" /> ... </intent-filter> Read more about writing intent filters in the Intents and Intent Filters document. Which option in the Zoom group would you use to display one entire page of a document in the document window?Choose a particular zoom setting. On the View tab, in the Zoom group, click Zoom 100%. ... . On the View tab, in the Zoom group, click One Page, Multiple Pages, or Page Width.. On the View tab, in the Zoom group, click Zoom, and then enter a percentage or choose any other settings that you want.. What is the shortcut to display the help pane?F1 Display the Help task pane. F6 Switch between the Help task pane and the active application. TAB Select the next item in the Help task pane. SHIFT+TAB Select the previous item in the Help task pane.
When ____ is enabled you can type without pressing the Enter key at the end of each line?EXAM MS WORD 2013. When using a touch screen a selection handle is a small circle that appears below the insertion point?When using a touch screen, a selection handle is a small circle that appears below the insertion point. Buttons and boxes on the HOME tab show formatting characteristics of the location of the insertion point. When the pointer is positioned in a selected text, its shape is a left-pointing block arrow.
|