Example usage for android.view MenuItem setOnMenuItemClickListener

List of usage examples for android.view MenuItem setOnMenuItemClickListener

Introduction

In this page you can find the example usage for android.view MenuItem setOnMenuItemClickListener.

Prototype

public MenuItem setOnMenuItemClickListener(MenuItem.OnMenuItemClickListener menuItemClickListener);

Source Link

Document

Set a custom listener for invocation of this menu item.

Usage

From source file:net.sourceforge.servestream.fragment.BrowseFragment.java

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

    // create menu to handle stream URLs

    // create menu to handle deleting and sharing lists      
    final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
    final UriBean uri = (UriBean) getListView().getAdapter().getItem(info.position);

    try {/*from   ww  w. j a va  2  s.c o  m*/
        final String streamURL = uri.getUri().toString();

        // set the menu title to the name attribute of the URL link
        menu.setHeaderTitle(uri.getNickname());

        // save the URL
        android.view.MenuItem save = menu.add(R.string.save_label);
        save.setOnMenuItemClickListener(new OnMenuItemClickListener() {
            public boolean onMenuItemClick(android.view.MenuItem arg0) {
                // prompt user to make sure they really want this
                new AlertDialog.Builder(BrowseFragment.this.getActivity())
                        .setMessage(getString(R.string.url_save_confirmation_msg, streamURL))
                        .setPositiveButton(R.string.confirm_label, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                saveUri(uri);
                                BrowseFragment.this.getActivity()
                                        .sendBroadcast(new Intent(UrlListFragment.UPDATE_LIST));
                            }
                        }).setNegativeButton(android.R.string.cancel, null).create().show();
                return true;
            }
        });

        // view the URL
        android.view.MenuItem view = menu.add(R.string.view_url_label);
        view.setOnMenuItemClickListener(new OnMenuItemClickListener() {
            public boolean onMenuItemClick(android.view.MenuItem arg0) {
                // display the URL
                new AlertDialog.Builder(BrowseFragment.this.getActivity()).setMessage(streamURL)
                        .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                return;
                            }
                        }).create().show();
                return true;
            }
        });

    } catch (Exception ex) {
        ex.printStackTrace();
    }

    // add to playlist
    android.view.MenuItem add = menu.add(R.string.add_to_playlist_label);
    add.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        public boolean onMenuItemClick(android.view.MenuItem item) {
            MusicUtils.addToCurrentPlaylistFromURL(BrowseFragment.this.getActivity(), uri, mQueueHandler);
            return true;
        }
    });

    // share the URL
    android.view.MenuItem share = menu.add(R.string.share_label);
    share.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        public boolean onMenuItemClick(android.view.MenuItem item) {
            String url = uri.getUri().toString();
            String appName = getString(R.string.app_name);

            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, getString(R.string.share_signature, url, appName));
            startActivity(Intent.createChooser(intent, getString(R.string.share_label)));
            return true;
        }
    });
}

From source file:com.koushikdutta.superuser.FragmentLog.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    VectorDrawableCompat icon = VectorDrawableCompat.create(getContext().getResources(), R.drawable.ic_delete,
            getContext().getTheme());/*from  w w  w. j  a va  2 s  . co  m*/

    if (!Theme.getThemeCurrent(PreferenceManager.getDefaultSharedPreferences(getContext()))
            .equals(MainActivity.PREF_BLACK_THEME)) {
        icon.setColorFilter(getActivity() instanceof ActivityLog ? ((ActivityLog) getActivity()).textToolbar
                : ((MainActivity) getActivity()).textToolbarDefault, PorterDuff.Mode.SRC_ATOP);

    } else {
        icon.setColorFilter(0xffeaeaea, PorterDuff.Mode.SRC_ATOP);
    }

    if (up == null) {
        MenuItem delete = menu.add(R.string.delete).setIcon(icon);
        delete.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

        delete.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if (up != null)
                    SuDatabaseHelper.delete(getActivity(), up);

                else
                    SuperuserDatabaseHelper.deleteLogs(getActivity());

                adapter.clear();
                callback.onLogCleared();
                //if (up != null) getActivity().finish();
                return true;
            }
        });
    }

}

From source file:com.audiokernel.euphonyrmt.fragments.BrowseFragment.java

@Override
public void onCreateContextMenu(final ContextMenu menu, final View v,
        final ContextMenu.ContextMenuInfo menuInfo) {
    final AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;

    final int index = (int) info.id;
    if (index >= 0 && mItems.size() > index) {
        menu.setHeaderTitle(mItems.get((int) info.id).toString());
        // If in simple mode, show "Play" (add, replace & play), "Add to queue" and "Add to playlist"
        if (mApp.isInSimpleMode()) {
            final MenuItem playItem = menu.add(ADD_REPLACE_PLAY, ADD_REPLACE_PLAY, 0, R.string.play);
            playItem.setOnMenuItemClickListener(this);
            final MenuItem addItem = menu.add(ADD, ADD, 0, R.string.addToQueue);
            addItem.setOnMenuItemClickListener(this);
        } else {/*from  ww w .  java2  s.c  o  m*/
            final MenuItem addItem = menu.add(ADD, ADD, 0, mIrAdd);
            addItem.setOnMenuItemClickListener(this);
            final MenuItem addAndReplaceItem = menu.add(ADD_REPLACE, ADD_REPLACE, 0, R.string.addAndReplace);
            addAndReplaceItem.setOnMenuItemClickListener(this);
            final MenuItem addAndReplacePlayItem = menu.add(ADD_REPLACE_PLAY, ADD_REPLACE_PLAY, 0,
                    R.string.addAndReplacePlay);
            addAndReplacePlayItem.setOnMenuItemClickListener(this);
            final MenuItem addAndPlayItem = menu.add(ADD_PLAY, ADD_PLAY, 0, R.string.addAndPlay);
            addAndPlayItem.setOnMenuItemClickListener(this);
        }

        if (R.string.addPlaylist != mIrAdd && R.string.addStream != mIrAdd
                && mApp.oMPDAsyncHelper.oMPD.isCommandAvailable(MPDCommand.MPD_CMD_LISTPLAYLISTS)) {

            int id = 0;
            final SubMenu playlistMenu = menu.addSubMenu(R.string.addToPlaylist);
            MenuItem item = playlistMenu.add(ADD_TO_PLAYLIST, id++, (int) info.id, R.string.newPlaylist);
            item.setOnMenuItemClickListener(this);

            try {
                final List<Item> playlists = mApp.oMPDAsyncHelper.oMPD.getPlaylists();

                if (null != playlists) {
                    for (final Item pl : playlists) {
                        item = playlistMenu.add(ADD_TO_PLAYLIST, id++, (int) info.id, pl.getName());
                        item.setOnMenuItemClickListener(this);
                    }
                }
            } catch (final IOException | MPDException e) {
                Log.e(TAG, "Failed to parse playlists.", e);
            }
        }
        final MenuItem gotoArtistItem = menu.add(GOTO_ARTIST, GOTO_ARTIST, 0, R.string.goToArtist);
        gotoArtistItem.setOnMenuItemClickListener(this);

    }
}

From source file:de.uni.stuttgart.informatik.ToureNPlaner.UI.Activities.MapScreen.MapScreen.java

private void setupGpsMenu(MenuItem item) {
    item.setChecked(gpsListener.isFollowing());
    item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override//from w ww. j a v  a 2s. c  om
        public boolean onMenuItemClick(MenuItem item) {
            item.setChecked(!item.isChecked());
            gpsListener.setFollowing(item.isChecked());
            MapScreen.this.invalidateOptionsMenu();
            return true;
        }
    });
}

From source file:org.samcrow.ridgesurvey.MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.home_menu, menu);

    final MenuItem compassItem = menu.findItem(R.id.compass_item);
    if (mHeadingCalculator.isAvailable()) {
        compassItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
            @Override/*  ww  w .j a  v a 2s .c  o m*/
            public boolean onMenuItemClick(MenuItem item) {
                item.setChecked(!item.isChecked());
                if (item.isChecked()) {
                    mCompass.setVisibility(View.VISIBLE);
                    mHeadingCalculator.resume();
                } else {
                    mCompass.setVisibility(View.INVISIBLE);
                    mHeadingCalculator.pause();
                }
                return true;
            }
        });
    } else {
        compassItem.setVisible(false);
    }

    final MenuItem editItem = menu.findItem(R.id.edit_item);
    editItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            final Site selectedSite = mSelectionManager.getSelectedSite();
            final Route selectedSiteRoute = mSelectionManager.getSelectedSiteRoute();
            if (selectedSite != null && selectedSiteRoute != null) {
                final Intent intent = new Intent(MainActivity.this, DataEntryActivity.class);
                intent.putExtra(DataEntryActivity.ARG_SITE, selectedSite);
                intent.putExtra(DataEntryActivity.ARG_ROUTE, selectedSiteRoute.getName());
                startActivityForResult(intent, REQUEST_CODE_ENTRY);
            } else {
                new AlertDialog.Builder(MainActivity.this).setTitle(R.string.no_site_selected)
                        .setMessage(R.string.select_a_site).setNeutralButton(android.R.string.ok, null).show();
            }
            return true;
        }
    });

    final MenuItem viewObservationsItem = menu.findItem(R.id.view_observations_item);
    viewObservationsItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            final Intent intent = new Intent(MainActivity.this, ObservationListActivity.class);
            startActivityForResult(intent, REQUEST_CODE_OBSERVATION_LIST);
            return true;
        }
    });

    final MenuItem uploadItem = menu.findItem(R.id.upload_item);
    uploadItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            // Start the upload service
            startUpload();
            return true;
        }
    });
    final UploadMenuItemController controller = new UploadMenuItemController(this, uploadItem);
    mUploadStatusTracker.addListener(controller);

    return true;
}

From source file:org.wikipedia.page.snippet.ShareHandler.java

private void handleSelection(Menu menu, MenuItem shareItem) {
    if (WikipediaApp.getInstance().getOnboardingStateMachine().isShareTutorialEnabled()) {
        postShowShareToolTip(shareItem);
        WikipediaApp.getInstance().getOnboardingStateMachine().setShareTutorial();
    }/*from   w ww  .jav a 2s. c  o m*/

    // Provide our own listeners for the copy, define, and share buttons.
    shareItem.setOnMenuItemClickListener(new RequestTextSelectOnMenuItemClickListener(PAYLOAD_PURPOSE_SHARE));
    MenuItem copyItem = menu.findItem(R.id.menu_text_select_copy);
    copyItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            fragment.getWebView().copyToClipboard();
            showCopySnackbar();
            leaveActionMode();
            return true;
        }
    });
    MenuItem defineItem = menu.findItem(R.id.menu_text_select_define);
    if (shouldEnableWiktionaryDialog()) {
        defineItem.setVisible(true);
        defineItem.setOnMenuItemClickListener(
                new RequestTextSelectOnMenuItemClickListener(PAYLOAD_PURPOSE_DEFINE));
    }
    MenuItem editItem = menu.findItem(R.id.menu_text_edit_here);
    editItem.setOnMenuItemClickListener(
            new RequestTextSelectOnMenuItemClickListener(PAYLOAD_PURPOSE_EDIT_HERE));
    if (!fragment.getPage().isArticle()) {
        editItem.setVisible(false);
    }

    createFunnel();
    funnel.logHighlight();
}

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

@Override
public void onCreateContextMenu(final ContextMenu menu, final View v,
        final ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    final AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;

    switch (mPager.getCurrentItem()) {
    case 0://from   w  w w  .  ja v a2  s. c o  m
        final Artist artist = mArtistResults.get((int) info.id);
        menu.setHeaderTitle(artist.mainText());
        setContextForObject(artist);
        break;
    case 1:
        final Album album = mAlbumResults.get((int) info.id);
        menu.setHeaderTitle(album.mainText());
        setContextForObject(album);
        break;
    case 2:
        final Music music = mSongResults.get((int) info.id);
        final MenuItem gotoAlbumItem = menu.add(Menu.NONE, GOTO_ALBUM, 0, R.string.goToAlbum);
        gotoAlbumItem.setOnMenuItemClickListener(this);
        menu.setHeaderTitle(music.mainText());
        setContextForObject(music);
        break;
    default:
        break;
    }

    final MenuItem addItem = menu.add(Menu.NONE, ADD, 0, getString(mAddString));
    final MenuItem addAndReplaceItem = menu.add(Menu.NONE, ADD_REPLACE, 0, R.string.addAndReplace);
    final MenuItem addReplacePlayItem = menu.add(Menu.NONE, ADD_REPLACE_PLAY, 0, R.string.addAndReplacePlay);
    final MenuItem addAndPlayItem = menu.add(Menu.NONE, ADD_PLAY, 0, R.string.addAndPlay);

    addItem.setOnMenuItemClickListener(this);
    addAndReplaceItem.setOnMenuItemClickListener(this);
    addReplacePlayItem.setOnMenuItemClickListener(this);
    addAndPlayItem.setOnMenuItemClickListener(this);
}

From source file:com.examples.abelanav2.ui.PicturesFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    MenuItem refreshMenu = menu.add(R.string.action_refresh);
    refreshMenu.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    refreshMenu.setOnMenuItemClickListener(this);
    super.onCreateOptionsMenu(menu, inflater);
}

From source file:com.googlecode.android_scripting.facade.ui.UiFacade.java

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    for (UiMenuItem item : mContextMenuItems) {
        MenuItem menuItem = menu.add(item.mmTitle);
        menuItem.setOnMenuItemClickListener(item.mmListener);
    }//ww  w .  j  a  v a  2  s.  c o  m
}

From source file:net.olejon.mdapp.NotesEditActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_notes_edit, menu);

    if (noteId != 0) {
        MenuItem menuItem = menu
                .add(Menu.NONE, Menu.NONE, Menu.NONE, getString(R.string.notes_edit_menu_delete))
                .setIcon(R.drawable.ic_delete_white_24dp);
        menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

        menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override/*  w ww  . j a v  a 2 s. c om*/
            public boolean onMenuItemClick(MenuItem menuItem) {
                deleteNote(false);
                return true;
            }
        });
    }

    return true;
}