Example usage for android.widget AdapterView showContextMenu

List of usage examples for android.widget AdapterView showContextMenu

Introduction

In this page you can find the example usage for android.widget AdapterView showContextMenu.

Prototype

public boolean showContextMenu() 

Source Link

Document

Shows the context menu for this view.

Usage

From source file:learn2crack.activities.WnContactsListFragment.java

@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    // Notifies the parent activity that the user selected a contact. In a two-pane layout, the
    // parent activity loads a ContactDetailFragment that displays the details for the selected
    // contact. In a single-pane layout, the parent activity starts a new activity that
    // displays contact details in its own Fragment.
    mOnContactSelectedListener.onContactSelected(null);

    registerForContextMenu(parent);/*  w ww  .  j a va  2  s  . c  o  m*/
    //parent.setTag(R.id.TAG_HAS_WN,v.getTag(R.id.TAG_HAS_WN));
    parent.setTag(R.id.TAG_CONTACT_ID, v.getTag(R.id.TAG_CONTACT_ID));
    parent.setTag(R.id.TAG_CONTACT, v.getTag(R.id.TAG_CONTACT));
    parent.showContextMenu();
    unregisterForContextMenu(parent);
}

From source file:learn2crack.activities.ContactsListFragment.java

@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    // Gets the Cursor object currently bound to the ListView
    final Cursor cursor = mAdapter.getCursor();

    // Moves to the Cursor row corresponding to the ListView item that was clicked
    cursor.moveToPosition(position);/*from   w w  w  .j a va  2s .  co  m*/

    // Creates a contact lookup Uri from contact ID and lookup_key
    final Uri uri = Contacts.getLookupUri(cursor.getLong(ContactsQuery.ID),
            cursor.getString(ContactsQuery.LOOKUP_KEY));

    // Notifies the parent activity that the user selected a contact. In a two-pane layout, the
    // parent activity loads a ContactDetailFragment that displays the details for the selected
    // contact. In a single-pane layout, the parent activity starts a new activity that
    // displays contact details in its own Fragment.
    mOnContactSelectedListener.onContactSelected(uri);

    registerForContextMenu(parent);
    parent.setTag(R.id.TAG_HAS_WN, v.getTag(R.id.TAG_HAS_WN));
    parent.setTag(R.id.TAG_CONTACT_ID, v.getTag(R.id.TAG_CONTACT_ID));
    parent.showContextMenu();
    unregisterForContextMenu(parent);

    // If two-pane layout sets the selected item to checked so it remains highlighted. In a
    // single-pane layout a new activity is started so this is not needed.
    if (mIsTwoPaneLayout) {
        getListView().setItemChecked(position, true);
    }
    //getListView().setItemChecked(position, false);
}

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();/*  ww  w . ja  va2  s .  com*/
                        return true;
                    } else if (getExplorer() != null)
                        return getExplorer().onIconContextItemSelected(pop, item, item.getMenuInfo(), view);
                    return false;
                }
            });
            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;
}