Example usage for android.widget PopupMenu dismiss

List of usage examples for android.widget PopupMenu dismiss

Introduction

In this page you can find the example usage for android.widget PopupMenu dismiss.

Prototype

public void dismiss() 

Source Link

Document

Dismiss the menu popup.

Usage

From source file:org.brandroid.openmanager.fragments.ContentFragment.java

public boolean createContextMenu(final OpenPath file, final AdapterView<?> list, final View view, final int pos,
        final int xOffset, final int yOffset) {
    Logger.LogInfo(getClassName() + ".onItemLongClick: " + file);

    final OpenContextMenuInfo info = new OpenContextMenuInfo(file);

    if (!OpenExplorer.USE_PRETTY_CONTEXT_MENUS) {
        if (Build.VERSION.SDK_INT > 10) {
            final PopupMenu pop = new PopupMenu(view.getContext(), view);
            pop.setOnMenuItemClickListener(new OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {
                    if (onOptionsItemSelected(item)) {
                        pop.dismiss();
                        return true;
                    } else if (getExplorer() != null)
                        return getExplorer().onIconContextItemSelected(pop, item, item.getMenuInfo(), view);
                    return false;
                }/*from w w  w .  j  av a  2 s. co m*/
            });
            pop.getMenuInflater().inflate(R.menu.context_file, pop.getMenu());
            onPrepareOptionsMenu(pop.getMenu());
            if (DEBUG)
                Logger.LogDebug("PopupMenu.show()");
            pop.show();
            return true;
        } else
            return list.showContextMenu();
    } else if (OpenExplorer.BEFORE_HONEYCOMB || !OpenExplorer.USE_ACTIONMODE) {

        try {
            //View anchor = view; //view.findViewById(R.id.content_context_helper);
            //if(anchor == null) anchor = view;
            //view.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            //Rect r = new Rect(view.getLeft(),view.getTop(),view.getMeasuredWidth(),view.getMeasuredHeight());
            MenuBuilder cmm = IconContextMenu.newMenu(list.getContext(), R.menu.context_file);
            if (!file.canRead()) {
                MenuUtils.setMenuEnabled(cmm, false);
                MenuUtils.setMenuEnabled(cmm, true, R.id.menu_context_info);
            }
            MenuUtils.setMenuEnabled(cmm, file.canWrite(), R.id.menu_context_paste, R.id.menu_context_cut,
                    R.id.menu_context_delete, R.id.menu_context_rename);
            onPrepareOptionsMenu(cmm);

            //if(!file.isArchive()) hideItem(cmm, R.id.menu_context_unzip);
            if (getClipboard().size() > 0)
                MenuUtils.setMenuVisible(cmm, false, R.id.menu_multi);
            else
                MenuUtils.setMenuVisible(cmm, false, R.id.menu_context_paste);
            MenuUtils.setMenuEnabled(cmm, !file.isDirectory(), R.id.menu_context_edit, R.id.menu_context_view);
            final IconContextMenu cm = new IconContextMenu(list.getContext(), cmm, view);
            //cm.setAnchor(anchor);
            cm.setNumColumns(2);
            cm.setOnIconContextItemSelectedListener(getExplorer());
            cm.setInfo(info);
            cm.setTextLayout(R.layout.context_item);
            cm.setTitle(file.getName());
            if (!cm.show()) //r.left, r.top);
                return list.showContextMenu();
            else
                return true;
        } catch (Exception e) {
            Logger.LogWarning("Couldn't show Iconified menu.", e);
            return list.showContextMenu();
        }
    }

    if (!OpenExplorer.BEFORE_HONEYCOMB && OpenExplorer.USE_ACTIONMODE) {
        if (!file.isDirectory() && mActionMode == null && !getClipboard().isMultiselect()) {
            try {
                Method mStarter = getActivity().getClass().getMethod("startActionMode");
                mActionMode = mStarter.invoke(getActivity(), new ActionModeHelper.Callback() {
                    //@Override
                    public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
                        return false;
                    }

                    //@Override
                    public void onDestroyActionMode(android.view.ActionMode mode) {
                        mActionMode = null;
                        mActionModeSelected = false;
                    }

                    //@Override
                    public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
                        mode.getMenuInflater().inflate(R.menu.context_file, menu);

                        mActionModeSelected = true;
                        return true;
                    }

                    //@Override
                    public boolean onActionItemClicked(android.view.ActionMode mode, MenuItem item) {
                        //ArrayList<OpenPath> files = new ArrayList<OpenPath>();

                        //OpenPath file = mLastPath.getChild(mode.getTitle().toString());
                        //files.add(file);

                        if (item.getItemId() != R.id.menu_context_cut && item.getItemId() != R.id.menu_multi
                                && item.getItemId() != R.id.menu_context_copy) {
                            mode.finish();
                            mActionModeSelected = false;
                        }
                        return executeMenu(item.getItemId(), mode, file);
                    }
                });
                Class cAM = Class.forName("android.view.ActionMode");
                Method mST = cAM.getMethod("setTitle", CharSequence.class);
                mST.invoke(mActionMode, file.getName());
            } catch (Exception e) {
                Logger.LogError("Error using ActionMode", e);
            }
        }
        return true;

    }

    if (file.isDirectory() && mActionMode == null && !getClipboard().isMultiselect()) {
        if (!OpenExplorer.BEFORE_HONEYCOMB && OpenExplorer.USE_ACTIONMODE) {
            try {
                Method mStarter = getActivity().getClass().getMethod("startActionMode");
                mActionMode = mStarter.invoke(getActivity(), new ActionModeHelper.Callback() {

                    //@Override
                    public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
                        return false;
                    }

                    //@Override
                    public void onDestroyActionMode(android.view.ActionMode mode) {
                        mActionMode = null;
                        mActionModeSelected = false;
                    }

                    //@Override
                    public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
                        mode.getMenuInflater().inflate(R.menu.context_file, menu);
                        menu.findItem(R.id.menu_context_paste).setEnabled(getClipboard().size() > 0);
                        //menu.findItem(R.id.menu_context_unzip).setEnabled(mHoldingZip);

                        mActionModeSelected = true;

                        return true;
                    }

                    //@Override
                    public boolean onActionItemClicked(android.view.ActionMode mode, MenuItem item) {
                        return executeMenu(item.getItemId(), mode, file);
                    }
                });
                Class cAM = Class.forName("android.view.ActionMode");
                Method mST = cAM.getMethod("setTitle", CharSequence.class);
                mST.invoke(mActionMode, file.getName());
            } catch (Exception e) {
                Logger.LogError("Error using ActionMode", e);
            }
        }

        return true;
    }

    return false;
}

From source file:org.brandroid.openmanager.activities.OpenExplorer.java

public boolean onIconContextItemSelected(PopupMenu menu, MenuItem item, ContextMenuInfo menuInfo, View view) {
    if (menu != null)
        menu.dismiss();
    if (onClick(item.getItemId(), item, view))
        return true;
    if (onOptionsItemSelected(item))
        return true;
    int index = Utils.getArrayIndex(MenuUtils.MENU_LOOKUP_IDS, item.getItemId());
    if (index > -1)
        return showMenu(MenuUtils.getMenuLookupSub(index), view, true);
    else {//w w  w  .j  a  va  2 s  .co  m
        View v = findViewById(item.getItemId());
        if (v != null && v.isClickable()) {
            onClick(v);
            return true;
        }
    }
    return false;
}