FileDocCategorySizeDatePackage
MenuDialogHelper.javaAPI DocAndroid 1.5 API4073Wed May 06 22:41:56 BST 2009com.android.internal.view.menu

MenuDialogHelper

public class MenuDialogHelper extends Object implements DialogInterface.OnClickListener, DialogInterface.OnKeyListener
Helper for menus that appear as Dialogs (context and submenus).
hide

Fields Summary
private MenuBuilder
mMenu
private android.widget.ListAdapter
mAdapter
private android.app.AlertDialog
mDialog
Constructors Summary
public MenuDialogHelper(MenuBuilder menu)

        mMenu = menu;
    
Methods Summary
public voiddismiss()
Dismisses the menu's dialog.

see
Dialog#dismiss()

        if (mDialog != null) {
            mDialog.dismiss();
        }
    
public voidonClick(android.content.DialogInterface dialog, int which)

        mMenu.performItemAction((MenuItemImpl) mAdapter.getItem(which), 0);
    
public booleanonKey(android.content.DialogInterface dialog, int keyCode, android.view.KeyEvent event)

        /*
         * Close menu on key down (more responsive, and there's no way to cancel
         * a key press so no point having it on key up. Note: This is also
         * needed because when a top-level menu item that shows a submenu is
         * invoked by chording, this onKey method will be called with the menu
         * up event.
         */
        if (event.getAction() == KeyEvent.ACTION_DOWN && (keyCode == KeyEvent.KEYCODE_MENU)
                || (keyCode == KeyEvent.KEYCODE_BACK)) {
            mMenu.close(true);
            dialog.dismiss();
            return true;
        }

        // Menu shortcut matching
        return mMenu.performShortcut(keyCode, event, 0);

    
public voidshow(android.os.IBinder windowToken)
Shows menu as a dialog.

param
windowToken Optional token to assign to the window.

        // Many references to mMenu, create local reference
        final MenuBuilder menu = mMenu;
        
        // Get an adapter for the menu item views
        mAdapter = menu.getMenuAdapter(MenuBuilder.TYPE_DIALOG);
        
        // Get the builder for the dialog
        final AlertDialog.Builder builder = new AlertDialog.Builder(menu.getContext())
                .setAdapter(mAdapter, this); 

        // Set the title
        final View headerView = menu.getHeaderView();
        if (headerView != null) {
            // Menu's client has given a custom header view, use it
            builder.setCustomTitle(headerView);
        } else {
            // Otherwise use the (text) title and icon
            builder.setIcon(menu.getHeaderIcon()).setTitle(menu.getHeaderTitle());
        }
        
        // Set the key listener
        builder.setOnKeyListener(this);

        // Since this is for a menu, disable the recycling of views
        // This is done by the menu framework anyway
        builder.setRecycleOnMeasureEnabled(false);
        
        // Show the menu
        mDialog = builder.create();
        
        WindowManager.LayoutParams lp = mDialog.getWindow().getAttributes();
        lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
        if (windowToken != null) {
            lp.token = windowToken;
        }
        lp.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
        
        mDialog.show();