Example usage for android.widget PopupMenu setOnMenuItemClickListener

List of usage examples for android.widget PopupMenu setOnMenuItemClickListener

Introduction

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

Prototype

public void setOnMenuItemClickListener(OnMenuItemClickListener listener) 

Source Link

Document

Sets a listener that will be notified when the user selects an item from the menu.

Usage

From source file:com.tlongdev.bktf.util.Utility.java

public static PopupMenu createItemPopupMenu(final Activity activity, View anchor, final Item item) {
    PopupMenu menu = new PopupMenu(activity, anchor);

    menu.getMenuInflater().inflate(R.menu.popup_item, menu.getMenu());

    menu.getMenu().findItem(R.id.favorite)
            .setTitle(isFavorite(activity, item) ? "Remove from favorites" : "Add to favorites");

    menu.getMenu().findItem(R.id.calculator).setEnabled(!isInCalculator(activity, item));

    menu.setOnMenuItemClickListener(new android.widget.PopupMenu.OnMenuItemClickListener() {
        @Override/*from  w ww .  jav a  2  s  .  com*/
        public boolean onMenuItemClick(MenuItem menuItem) {
            switch (menuItem.getItemId()) {
            case R.id.history:
                Intent i = new Intent(activity, PriceHistoryActivity.class);
                i.putExtra(PriceHistoryActivity.EXTRA_ITEM, item);
                activity.startActivity(i);
                break;
            case R.id.favorite:
                if (isFavorite(activity, item)) {
                    removeFromFavorites(activity, item);
                } else {
                    addToFavorites(activity, item);
                }
                break;
            case R.id.calculator:
                addToCalculator(activity, item);
                menuItem.setEnabled(false);
                break;
            case R.id.backpack_tf:
                CustomTabActivityHelper.openCustomTab(activity, new CustomTabsIntent.Builder().build(),
                        Uri.parse(item.getBackpackTfUrl()), new WebViewFallback());
                break;
            case R.id.wiki:
                CustomTabActivityHelper.openCustomTab(activity, new CustomTabsIntent.Builder().build(),
                        Uri.parse(item.getTf2WikiUrl()), new WebViewFallback());
                break;
            }
            return true;
        }
    });

    return menu;
}

From source file:MainActivity.java

public void showPopupMenu(View view) {
    PopupMenu popupMenu = new PopupMenu(MainActivity.this, view);
    popupMenu.inflate(R.menu.menu_popup);
    popupMenu.setOnMenuItemClickListener(mOnMenuItemClickListener);
    popupMenu.show();/* w ww.ja  va2 s  .c o  m*/
}

From source file:com.battlelancer.seriesguide.ui.MoviesWatchListFragment.java

@Override
public void onPopupMenuClick(View v, final int movieTmdbId) {
    PopupMenu popupMenu = new PopupMenu(v.getContext(), v);
    popupMenu.getMenu().add(0, CONTEXT_WATCHLIST_REMOVE_ID, 0, R.string.watchlist_remove);
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override//from w ww.j  av a2 s  .  co  m
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
            case CONTEXT_WATCHLIST_REMOVE_ID: {
                MovieTools.removeFromWatchlist(getActivity(), movieTmdbId);
                fireTrackerEvent("Remove from watchlist");
                return true;
            }
            }
            return false;
        }
    });
    popupMenu.show();
}

From source file:com.battlelancer.seriesguide.ui.MoviesCollectionFragment.java

@Override
public void onPopupMenuClick(View v, final int movieTmdbId) {
    PopupMenu popupMenu = new PopupMenu(v.getContext(), v);
    popupMenu.getMenu().add(0, CONTEXT_COLLECTION_REMOVE_ID, 0, R.string.action_collection_remove);
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override//from  w ww.j a  v  a2 s . c om
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
            case CONTEXT_COLLECTION_REMOVE_ID: {
                MovieTools.removeFromCollection(getActivity(), movieTmdbId);
                fireTrackerEvent("Remove from collection");
                return true;
            }
            }
            return false;
        }
    });
    popupMenu.show();
}

From source file:org.y20k.transistor.helpers.StationContextMenu.java

public void show() {

    PopupMenu popup = new PopupMenu(mActivity, mView);
    popup.inflate(R.menu.menu_main_list_item);
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override//w  w w .j a v  a2 s  . c o m
        public boolean onMenuItemClick(MenuItem item) {

            switch (item.getItemId()) {

            // CASE ICON
            case R.id.menu_icon:

                // send local broadcast (needed by MainActivityFragment)
                Intent i = new Intent();
                i.setAction(ACTION_IMAGE_CHANGE_REQUESTED);
                i.putExtra(STATION_ID, mStationID);
                LocalBroadcastManager.getInstance(mActivity.getApplication()).sendBroadcast(i);

                return true;

            // CASE RENAME
            case R.id.menu_rename:
                // get name of station
                String stationName = mCollection.getStations().get(mStationID).getStationName();
                // construct rename dialog
                DialogRename dialogRename = new DialogRename(mActivity, mCollection, stationName, mStationID);
                dialogRename.setStationRenamedListener(new DialogRename.StationRenamedListener() {
                    @Override
                    public void stationRenamed() {
                        if (mStationChangedListener != null) {
                            mStationChangedListener.stationChanged();
                        }
                    }
                });
                // run dialog
                dialogRename.show();
                return true;

            // CASE DELETE
            case R.id.menu_delete:
                // construct delete dialog
                DialogDelete dialogDelete = new DialogDelete(mActivity, mCollection, mStationID);
                // run dialog
                dialogDelete.show();
                return true;

            // CASE DEFAULT
            default:
                return false;
            }
        }
    });
    popup.show();
}

From source file:com.ultramegasoft.flavordex2.fragment.PhotoFragment.java

/**
 * Show the PopupMenu for the photo.//ww w .j  a v a2s .c o m
 *
 * @param v The View to attach the menu to
 */
private void showMenu(@NonNull View v) {
    final PopupMenu popupMenu = new PopupMenu(getContext(), v);
    popupMenu.setOnMenuItemClickListener(this);
    popupMenu.inflate(R.menu.photo_menu);
    popupMenu.show();
}

From source file:at.ac.uniklu.mobile.sportal.DashboardActivity.java

@TargetApi(11)
@Override//from  w  w  w .  j  av  a 2 s .co  m
public void openOptionsMenu() {
    // Honeycomb and ICS tablet menu workaround
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        /* Android >= 3 with holo theme doesn't support the options menu, so 
         * display a popup menu below the overflow action button. */
        PopupMenu m = new PopupMenu(this, findViewById(R.id.actionbar_overflow));
        m.getMenuInflater().inflate(R.menu.dashboard_menu, m.getMenu());
        m.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                return onOptionsItemSelected(item);
            }
        });
        m.show();
    } else {
        // show default options menu on Android 2.x
        super.openOptionsMenu();
    }
}

From source file:za.ac.uct.cs.lwsjam005.eshelf.activities.ReadActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
    case android.R.id.home:
        // go back home

        // ensure we are resuming the existing activity
        Intent intent = new Intent(this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(ARG_CURRENT_PAGE, currentPage);
        intent.putExtra(ARG_BOOKMARKS, thisBook.getBookmarks());
        intent.setAction(MainActivity.ACTION_RETURN_FROM_READ);

        startActivity(intent);//from   w w w  .j av a2  s . c o m

        return true;

    case R.id.action_previous:
        // Go to the previous step in the wizard. If there is no previous
        // step,
        // setCurrentItem will do nothing.
        mPager.setCurrentItem(mPager.getCurrentItem() - 1);
        return true;

    case R.id.action_next:
        // Advance to the next step in the wizard. If there is no next step,
        // setCurrentItem
        // will do nothing.
        mPager.setCurrentItem(mPager.getCurrentItem() + 1);
        return true;
    case R.id.action_bookmarks:
        PopupMenu popup = new PopupMenu(this, this.findViewById(R.id.action_bookmarks));
        popup.setOnMenuItemClickListener(this);
        MenuInflater inflater = popup.getMenuInflater();
        inflater.inflate(R.menu.popup_menu_bookmarks, popup.getMenu());

        popup.show();

        return true;
    }

    return super.onOptionsItemSelected(item);
}

From source file:org.hawkular.client.android.fragment.FavTriggersFragment.java

private void showTriggerMenu(final View triggerView, final int triggerPosition) {
    PopupMenu triggerMenu = new PopupMenu(getActivity(), triggerView);

    triggerMenu.getMenuInflater().inflate(R.menu.popup_delete, triggerMenu.getMenu());

    triggerMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override/*from w ww  . ja va  2  s. c om*/
        public boolean onMenuItemClick(MenuItem menuItem) {
            Trigger trigger = getTriggersAdapter().getItem(triggerPosition);

            switch (menuItem.getItemId()) {
            case R.id.menu_delete:
                Context context = getActivity();
                SQLStore<Trigger> store = openStore(context);
                store.openSync();
                store.remove(trigger.getId());
                onRefresh();
                return true;

            default:
                return false;
            }
        }
    });

    triggerMenu.show();
}

From source file:app.umitems.greenclock.DeskClockFragment.java

/**
 * Installs click and touch listeners on a fake overflow menu button.
 *
 * @param menuButton the fragment's fake overflow menu button
 *//*from w ww  .ja va2 s.  c  o  m*/
public void setupFakeOverflowMenuButton(View menuButton) {
    final PopupMenu fakeOverflow = new PopupMenu(menuButton.getContext(), menuButton) {
        @Override
        public void show() {
            getActivity().onPrepareOptionsMenu(getMenu());
            super.show();
        }
    };
    fakeOverflow.inflate(R.menu.desk_clock_menu);
    fakeOverflow.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            return getActivity().onOptionsItemSelected(item);
        }
    });

    menuButton.setOnTouchListener(PopupMenuCompat.getDragToOpenListener(fakeOverflow));
    menuButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            fakeOverflow.show();
        }
    });
}