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.pranavpandey.smallapp.sample.SmallAppSample.java

private void setupOptionMenu() {
    View header = LayoutInflater.from(this).inflate(R.layout.header, new LinearLayout(this), false);

    final View optionMenu = header.findViewById(R.id.option_menu);

    optionMenu.setOnLongClickListener(new View.OnLongClickListener() {
        @Override/* w  w  w .ja va 2s .com*/
        public boolean onLongClick(View v) {
            SmallTheme.getInstance().showHeaderHint(v, R.string.sas_options);
            return false;
        }
    });

    optionMenu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PopupMenu popup = new PopupMenu(getContext(), optionMenu);
            try {
                Field[] fields = popup.getClass().getDeclaredFields();
                for (Field field : fields) {
                    if ("mPopup".equals(field.getName())) {
                        field.setAccessible(true);
                        Object menuPopupHelper = field.get(popup);
                        Class<?> classPopupHelper = Class.forName(menuPopupHelper.getClass().getName());
                        Method setForceIcons = classPopupHelper.getMethod("setForceShowIcon", boolean.class);
                        setForceIcons.invoke(menuPopupHelper, true);
                        break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            popup.getMenuInflater().inflate(R.menu.main, popup.getMenu());
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                    case R.id.menu_settings:
                        showSettings();
                        break;

                    case R.id.menu_about:
                        new AboutDialog(SmallAppSample.this, R.mipmap.ic_launcher, R.string.app_name, Type.LIST)
                                .show(getRootView());
                        break;

                    case R.id.menu_sources:
                        SmallUtils.openLink(SmallAppSample.this, getRootView(), SOURCES_LINK, null, Type.GRID);
                        break;
                    }

                    return true;
                }
            });
            popup.show();
        }
    });
    getWindow().setHeaderView(header);
}

From source file:com.android.music.MusicBrowserActivity.java

/**
 * M: Create fake menu./*  www  .ja v  a  2s.  com*/
 */
private void createFakeMenu() {

    if (mPermissionReqProcessed == true) {
        if (mHasMenukey) {
            MusicLogUtils.d(TAG, "createFakeMenu Quit when there has Menu Key");
            return;
        }
        if (mOrientaiton == Configuration.ORIENTATION_LANDSCAPE) {
            mOverflowMenuButtonId = R.id.overflow_menu;
            mOverflowMenuButton = findViewById(R.id.overflow_menu);
        } else {
            mOverflowMenuButtonId = R.id.overflow_menu_nowplaying;
            mOverflowMenuButton = findViewById(R.id.overflow_menu_nowplaying);
            View parent = (View) mOverflowMenuButton.getParent();
            if (parent != null) {
                parent.setVisibility(View.VISIBLE);
            }
        }
        mOverflowMenuButton.setVisibility(View.VISIBLE);
        mOverflowMenuButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                MusicLogUtils.d(TAG, "createFakeMenu:onClick()");
                if (v.getId() == mOverflowMenuButtonId) {
                    final PopupMenu popupMenu = new PopupMenu(MusicBrowserActivity.this, mOverflowMenuButton);
                    mPopupMenu = popupMenu;
                    final Menu menu = popupMenu.getMenu();
                    onCreateOptionsMenu(menu);
                    popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
                        public boolean onMenuItemClick(MenuItem item) {
                            return onOptionsItemSelected(item);
                        }
                    });
                    popupMenu.setOnDismissListener(new OnDismissListener() {
                        public void onDismiss(PopupMenu menu) {
                            mPopupMenuShowing = false;
                            MusicLogUtils.d(TAG, "createFakeMenu:onDismiss() called");
                            return;
                        }
                    });
                    onPrepareOptionsMenu(menu);
                    mPopupMenuShowing = true;
                    if (popupMenu != null) {
                        MusicLogUtils.d(TAG, "createFakeMenu:popupMenu.show()");
                        popupMenu.show();
                    }
                }
            }
        });
    }
}

From source file:com.fastbootmobile.encore.app.adapters.SearchAdapter.java

private void showAlbumOverflow(final Context context, View parent, final Album album) {
    PopupMenu popupMenu = new PopupMenu(context, parent);
    popupMenu.inflate(R.menu.search_res_album);
    popupMenu.show();//from   w ww. j a  va2 s  . co  m

    final String artist = Utils.getMainArtist(album);

    if (artist == null) {
        // No artist could be found for this album, don't show the entry
        popupMenu.getMenu().removeItem(R.id.menu_open_artist_page);
    }

    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            switch (menuItem.getItemId()) {
            case R.id.menu_play_now:
                PlaybackProxy.playAlbum(album);
                break;

            case R.id.menu_add_to_queue:
                PlaybackProxy.queueAlbum(album, false);
                break;

            case R.id.menu_open_artist_page:
                if (artist != null) {
                    Intent intent = ArtistActivity.craftIntent(context, null, artist, album.getProvider(),
                            0xFF333333);
                    context.startActivity(intent);
                }
                break;

            case R.id.menu_add_to_playlist:
                PlaylistChooserFragment fragment = PlaylistChooserFragment.newInstance(album);
                fragment.show(((FragmentActivity) context).getSupportFragmentManager(), album.getRef());
                break;

            default:
                return false;
            }
            return true;
        }
    });
}

From source file:com.javielinux.fragments.MyActivityFragment.java

private void showMenuMoreActions(View v) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        PopupMenu popupMenu = new PopupMenu(getActivity(), v);
        popupMenu.getMenuInflater().inflate(R.menu.my_activity_more_actions, popupMenu.getMenu());
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

            @Override//from ww w  .  jav a 2  s. c om
            public boolean onMenuItemClick(MenuItem item) {
                if (item.getItemId() == R.id.popupmenu_more_actions_saved_tweet) {
                    openSavedTweetColumn();
                } else if (item.getItemId() == R.id.popupmenu_more_actions_trending_topics) {
                    newTrending();
                }
                return true;
            }
        });
        popupMenu.show();
    } else {
        AlertDialogFragment frag = new AlertDialogFragment();
        Bundle args = new Bundle();
        args.putInt(AlertDialogFragment.KEY_ALERT_TITLE, R.string.actions);
        args.putBoolean(AlertDialogFragment.KEY_ALERT_HAS_POSITIVE_BUTTON, false);
        args.putBoolean(AlertDialogFragment.KEY_ALERT_CANCELABLE, false);
        args.putInt(AlertDialogFragment.KEY_ALERT_ARRAY_ITEMS, R.array.popupmenu_my_activity_more_actions);
        frag.setArguments(args);
        frag.setAlertButtonListener(new AlertDialogFragment.AlertButtonListener() {
            @Override
            public void OnAlertButtonOk() {
            }

            @Override
            public void OnAlertButtonCancel() {
            }

            @Override
            public void OnAlertButtonNeutral() {
            }

            @Override
            public void OnAlertItems(int which) {
                if (which == 0) {
                    openSavedTweetColumn();
                } else if (which == 1) {
                    newTrending();
                }
            }
        });
        frag.show(getFragmentManager(), "dialog");
    }
}

From source file:com.javielinux.fragments.MyActivityFragment.java

private void showMenuOptions(View v) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        PopupMenu popupMenu = new PopupMenu(getActivity(), v);
        popupMenu.getMenuInflater().inflate(R.menu.my_activity_more_options, popupMenu.getMenu());
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

            @Override//  w  w w.j a v  a2s  . c om
            public boolean onMenuItemClick(MenuItem item) {
                if (item.getItemId() == R.id.popupmenu_more_options_size) {
                    showSizeText();
                } else if (item.getItemId() == R.id.popupmenu_more_options_theme) {
                    showThemeDialog();
                } else if (item.getItemId() == R.id.popupmenu_more_options_preferences) {
                    Intent i = new Intent(getActivity(), Preferences.class);
                    startActivity(i);
                } else if (item.getItemId() == R.id.popupmenu_more_options_exit) {
                    showDialogExit();
                }
                return true;
            }
        });
        popupMenu.show();
    } else {
        AlertDialogFragment frag = new AlertDialogFragment();
        Bundle args = new Bundle();
        args.putInt(AlertDialogFragment.KEY_ALERT_TITLE, R.string.actions);
        args.putBoolean(AlertDialogFragment.KEY_ALERT_HAS_POSITIVE_BUTTON, false);
        args.putBoolean(AlertDialogFragment.KEY_ALERT_CANCELABLE, false);
        args.putInt(AlertDialogFragment.KEY_ALERT_ARRAY_ITEMS, R.array.popupmenu_my_activity_more_options);
        frag.setArguments(args);
        frag.setAlertButtonListener(new AlertDialogFragment.AlertButtonListener() {
            @Override
            public void OnAlertButtonOk() {
            }

            @Override
            public void OnAlertButtonCancel() {
            }

            @Override
            public void OnAlertButtonNeutral() {
            }

            @Override
            public void OnAlertItems(int which) {
                if (which == 0) {
                    showSizeText();
                } else if (which == 1) {
                    Intent i = new Intent(getActivity(), Preferences.class);
                    startActivity(i);
                } else if (which == 2) {
                    showThemeDialog();
                } else if (which == 3) {
                    showDialogExit();
                }
            }
        });
        frag.show(getFragmentManager(), "dialog");
    }
}

From source file:com.audiokernel.euphonyrmt.MainMenuActivity.java

private PopupMenu initializeHeaderOverflowPopup(final View headerOverflowMenu) {
    final PopupMenu resultPopupMenu;

    if (headerOverflowMenu == null) {
        resultPopupMenu = null;//from w w  w.  j  a v a2 s . c o  m
    } else {
        final PopupMenu popupMenu = new PopupMenu(this, headerOverflowMenu);
        popupMenu.getMenuInflater().inflate(R.menu.mpd_mainmenu, popupMenu.getMenu());
        popupMenu.getMenuInflater().inflate(R.menu.mpd_playlistmenu, popupMenu.getMenu());
        popupMenu.getMenu().removeItem(R.id.PLM_EditPL);
        popupMenu.setOnMenuItemClickListener(this);

        headerOverflowMenu.setOnTouchListener(PopupMenuCompat.getDragToOpenListener(popupMenu));

        headerOverflowMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                if (mSlidingLayout != null && mSlidingLayout.isPanelExpanded()) {
                    prepareNowPlayingMenu(popupMenu.getMenu());
                    popupMenu.show();
                }
            }
        });

        resultPopupMenu = popupMenu;
    }

    return resultPopupMenu;
}

From source file:com.fastbootmobile.encore.app.adapters.SearchAdapter.java

private void showSongOverflow(final Context context, View parent, final Song song) {
    PopupMenu popupMenu = new PopupMenu(context, parent);
    popupMenu.inflate(R.menu.search_res_song);
    popupMenu.show();// w  w  w  .j  av a 2s.  c  om

    if (song.getArtist() == null) {
        // No attached artist, don't show the menu entry
        popupMenu.getMenu().removeItem(R.id.menu_open_artist_page);
    }

    if (song.getAlbum() == null) {
        // No attached album, don't show the menu entry
        popupMenu.getMenu().removeItem(R.id.menu_open_album_page);
    }

    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            Intent intent;
            switch (menuItem.getItemId()) {
            case R.id.menu_play_now:
                PlaybackProxy.playSong(song);
                break;

            case R.id.menu_add_to_queue:
                PlaybackProxy.queueSong(song, false);
                break;

            case R.id.menu_open_artist_page:
                if (song.getArtist() != null) {
                    intent = ArtistActivity.craftIntent(context, null, song.getArtist(), song.getProvider(),
                            0xFF333333);
                    context.startActivity(intent);
                }
                break;

            case R.id.menu_open_album_page:
                if (song.getAlbum() != null) {
                    intent = AlbumActivity.craftIntent(context, null, song.getAlbum(), song.getProvider(),
                            0xFF333333);
                    context.startActivity(intent);
                }
                break;

            case R.id.menu_add_to_playlist:
                PlaylistChooserFragment fragment = PlaylistChooserFragment.newInstance(song);
                fragment.show(((FragmentActivity) context).getSupportFragmentManager(), song.getRef());
                break;

            default:
                return false;
            }
            return true;
        }
    });
}

From source file:nz.ac.auckland.lablet.ExperimentRunViewManager.java

/**
 * Show a menu to enable or disable sensors.
 */// w ww  .  j a  va2 s . c o m
private void showSensorMenu() {
    View menuView = findViewById(R.id.action_sensors);
    PopupMenu popup = new PopupMenu(menuView.getContext(), menuView);

    final List<ISensorPlugin> plugins = ExperimentPluginFactory.getFactory().getSensorPlugins();
    for (int i = 0; i < plugins.size(); i++) {
        ISensorPlugin plugin = plugins.get(i);

        MenuItem item = popup.getMenu().add(1, i, i, plugin.getSensorName());
        item.setCheckable(true);

        if (getExperiment(plugin) != null)
            item.setChecked(true);
    }

    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            ISensorPlugin plugin = plugins.get(menuItem.getItemId());
            IExperimentSensor experimentSensor = getExperiment(plugin);
            if (experimentSensor != null)
                removeExperimentRun(experimentSensor);
            else
                addExperiment(plugin);
            return true;
        }
    });

    popup.show();
}

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

@Override
public void onPopupMenuClick(View v, final int movieTmdbId) {
    PopupMenu popupMenu = new PopupMenu(v.getContext(), v);
    popupMenu.inflate(R.menu.movies_popup_menu);

    // check if movie is already in watchlist or collection
    boolean isInWatchlist = false;
    boolean isInCollection = false;
    Cursor movie = getActivity().getContentResolver().query(
            SeriesGuideContract.Movies.buildMovieUri(movieTmdbId),
            new String[] { SeriesGuideContract.Movies.IN_WATCHLIST, SeriesGuideContract.Movies.IN_COLLECTION },
            null, null, null);//  ww  w  . j a v a2  s  . com
    if (movie != null) {
        if (movie.moveToFirst()) {
            isInWatchlist = movie.getInt(0) == 1;
            isInCollection = movie.getInt(1) == 1;
        }
        movie.close();
    }

    Menu menu = popupMenu.getMenu();
    menu.findItem(R.id.menu_action_movies_watchlist_add).setVisible(!isInWatchlist);
    menu.findItem(R.id.menu_action_movies_watchlist_remove).setVisible(isInWatchlist);
    menu.findItem(R.id.menu_action_movies_collection_add).setVisible(!isInCollection);
    menu.findItem(R.id.menu_action_movies_collection_remove).setVisible(isInCollection);

    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
            case R.id.menu_action_movies_watchlist_add: {
                MovieTools.addToWatchlist(getActivity(), movieTmdbId);
                fireTrackerEvent("Add to watchlist");
                return true;
            }
            case R.id.menu_action_movies_watchlist_remove: {
                MovieTools.removeFromWatchlist(getActivity(), movieTmdbId);
                fireTrackerEvent("Remove from watchlist");
                return true;
            }
            case R.id.menu_action_movies_collection_add: {
                MovieTools.addToCollection(getActivity(), movieTmdbId);
                fireTrackerEvent("Add to collection");
                return true;
            }
            case R.id.menu_action_movies_collection_remove: {
                MovieTools.removeFromCollection(getActivity(), movieTmdbId);
                fireTrackerEvent("Remove from collection");
                return true;
            }
            }
            return false;
        }
    });
    popupMenu.show();
}