Android user interface-menu

Source: Internet
Author: User
Tags gmail mail

A pop-up menu is a mode menu docked on a view. If there is space below the view object, the pop-up menu will be displayed below the docked object, otherwise it will be displayed above. This is very useful:

1. Provide an overflow menu for operations on the specified content (the Gmail mail header shown in 4 ).

Figure 4. A pop-up menu in the Gmail application stops at the overflow button in the upper right corner.

Note: different from context menus, context menus are operations that affect the selected content. For operations on the Content selected by the application, use the context operation mode or floating content menu.

2. Provide the second part of the command statement (for example, if a button is marked as "add", different "add" options are generated using the pop-up menu ).

3. Provides a drop-down menu similar to the spinner component that does not retain the persistent choice.

Note: The pop-up menu is valid at API level 11 and later.

If you define your menu in XML, the following shows how to display the pop-up menu:

1. Use its constructor to instantiate a popupmenu object, which requires the current applicationProgramThe context and view objects to be docked in the menu are used as parameters.

2. Use the menuinflater object to load your menu resources to the menu object returned by the popupmenu. getmenu () method. You can use the popupmenu. Inflate () method to replace the version above the API Level 14.

3. Call the popupmenu. Show () method to display the pop-up menu.

For example, the following is a pop-up menu display method with the Android: onclick attribute:

Definition in the configuration file:

<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"/>

In ActivityCode:

Public void showpopup (view v ){
Popupmenu popup = new popupmenu (this, V );
Menuinflater Inflater = Popup. getmenuinflater ();
Inflater. Inflate (R. Menu. Actions, Popup. getmenu ());
Popup. Show ();
}

In API Level 14 and later versions, you can use the popupmenu. Inflate () method to merge the two codes of the filling menu.

When you select a menu item or touch the outside of the menu area, the menu disappears. You can use the popupmenu. ondismisslistener callback method to listen for menu disappearance events.

Handling click events

When you select a menu item to perform an operation, you must implement the popupmenu. onmenuitemclicklistener interface and register it to your popupmenu object by calling the setonmenuitemclicklistener () method. If you select a project, the system will call the onmenuitemclick () callback method in your interface. For example:

Public void showmenu (view v ){
Popupmenu popup = new popupmenu (this, V );
// This activity implements onmenuitemclicklistener
Popup. setonmenuitemclicklistener (this );
Popup. Inflate (R. Menu. Actions );
Popup. Show ();
}
@ Override
Public Boolean onmenuitemclick (menuitem item ){
Switch (item. getitemid ()){
Case R. Id. Archive:
Archive (item );
Return true;
Case R. Id. Delete:
Delete (item );
Return true;
Default:
Return false;
}
}

Create a menu Group

A menu group is a set of menu items that share certain features. You can use a menu group to do the following:

1. Use setgroupvisible () to display or hide all menu items in the group;

2. Use setgroupenabled () to enable or disable all menu items in the group;

3. Use the setgroupcheckable () method to specify whether projects in all groups can be checked.

You can create a group menu by embedding the <item> element in the <group> element in the menu resource, or use the add () method with the group ID.

The following are menu resources that contain a group:



Android: icon = "@ drawable/menu_save"
Android: Title = "@ string/menu_save"/>


Android: Title = "@ string/menu_archive"/>
Android: title = "@ string/menu_delete"/>

The items in the group menu are displayed at the same level as the first item-the three items in the menu are at the same level. You can use the methods listed above, modify the attributes of two items in the group menu by referencing the group ID. The system does not separate the group menu items. For example, if you declare the Android: showasaction = "ifroom" attribute for each menu item, they can be both displayed in the Action column and in the action overflow.

Use a checkable menu item

You can use a checkbox menu to switch between different options, for a set of mutually exclusive options, you can use a set of menus with single-choice buttons (5 ).

Figure 5. submenus with check menu items.

Note: A check box or single-choice button cannot be displayed for menu items in the icon menu (option-type menu. If you select to make the menu items in the icon menu check, you must change the manual change icon or text to specify the check status each time the status changes.

You can use the Android: checkable attribute in the <item> element to define a checkable behavior for a separate menu item, or use the Android: the checkablebehavior attribute defines a checkable behavior for a group of menu items. For example, in the following example, each menu item in the menu group contains a check button that can be checked:

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

Android: The checkablebehavior attribute can be set as follows:

1. Single: Only one menu in the menu group can be checked (Check button)

2. All: All menu items can be checked (check box)

3. None: no items can be checked.

You can use the Android: checked attribute in the <item> element to set the default check status for a menu item, and use the setchecked () method to change it in the code.

When a checkable menu item is selected, the system calls the callback method (such as onoptionsitemselected () for the selected menu item ()). You must set the status of check boxes at this time, because check boxes or check buttons do not automatically change their status. You can use the ischecked () method to query the current status of the check menu (the selected status), and then use the setchecked () method to set the check status. For example:

@ Override
Public Boolean onoptionsitemselected (menuitem item ){
Switch (item. getitemid ()){
Case R. Id. vibrate:
Case R. Id. dont_vibrate:
If (item. ischecked () item. setchecked (false );
Else item. setchecked (true );
Return true;
Default:
Return super. onoptionsitemselected (item );
}
}

If you do not need to set the check status in this way, the visual status of the selected menu item (check box or check button) will not change. When you set this status, the activity retains the menu status, so that when you open this menu, the check status you set is visible.

Note: You can use the check menu item only on the basis of each session, and do not save the check status after the application is destroyed. If your application intends to save user settings, you should use the shared method to save data.

Add menu items based on intent objects

Sometimes you want the menu item to use an intent object to start an activity (whether in your application or other applications ).

When you know the intent object you want to use and specify the menu item to start this intent object, you can use the startactivity () method to execute the intent object during the corresponding on-item-selected callback method (such as the onoptionsitemselected () callback method) Call.

However, if you are not sure whether your device contains an application that processes this intent object, adding a menu item that calls this intent object may lead to a non-functional menu, because the intent object activity may not be accepted. To solve this problem, Android allows you to dynamically add menu items to the menu when searching and processing the activity of your intent object on the device.

The following describes how to add a menu item based on the activity that can accept the valid intent object:

1. Use category_alternative or category_selected_alternative to define an intent object.

2. Call the menu. addintentoptions () method. Android will search for any application that can accept this intent object in the system and add them to your menu.

If no application meets the intent requirements is installed, no menu items are added.

Note: category_selected_alternative is used to process the currently selected elements on the screen. Therefore, this category should be used only when the oncreatecontextmenu () method is used to create a menu.

For example:

@ Override
Public Boolean oncreateoptionsmenu (menu ){
Super. oncreateoptionsmenu (menu );
// Create an intent that describes the requirements to fulfill, to be encoded
// In our menu. The offering app must include a category value of intent. category_alternative.
Intent intent = new intent (null, datauri );
Intent. addcategory (intent. category_alternative );
// Search and populate the menu with acceptable offering applications.
Menu. addintentoptions (
R. Id. intent_group, // menu group to which new items will be added
0, // unique item ID (none)
0, // order for the items (none)
This. getcomponentname (), // The current activity name
Null, // specific items to place first (none)
Intent, // intent created above that describes our requirements
0, // additional flags to control items (none)
Null); // array of menuitems that correlate to specific items (none)
Return true;
}

For each activity that provides an intent filter that matches the defined intent object, a menu item is added. This menu item uses the android of the intent filter: the label attribute value is used as the title of the menu item and the application icon as the icon of the menu item. The addintentoptions () method returns the number of added menus.

Note: When you call the addintentoptions () method, it overwrites all menu items in the menu group specified in the first parameter.

Allow your activity to be added to other menus

You can also provide activity services to other applications so that your applications can be included in the menus of other applications.

To include your application in other Application menus, You need to define an intent filter as usual, but confirm that the category includes category_alternative or category_selected_alternative, such:


...


...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.