Example usage for android.view ContextMenu add

List of usage examples for android.view ContextMenu add

Introduction

In this page you can find the example usage for android.view ContextMenu add.

Prototype

public MenuItem add(int groupId, int itemId, int order, CharSequence title);

Source Link

Document

Add a new item to the menu.

Usage

From source file:com.andrewshu.android.reddit.comments.CommentsListActivity.java

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

    ThingInfo item = mCommentsAdapter.getItem(rowId);

    if (rowId == 0) {
        menu.add(0, Constants.SHARE_CONTEXT_ITEM, Menu.NONE, "Share");

        if (getOpThingInfo().isSaved()) {
            menu.add(0, Constants.UNSAVE_CONTEXT_ITEM, Menu.NONE, "Unsave");
        } else {//from w w  w. j av a  2  s .c o  m
            menu.add(0, Constants.SAVE_CONTEXT_ITEM, Menu.NONE, "Save");
        }
        if (getOpThingInfo().isHidden()) {
            menu.add(0, Constants.UNHIDE_CONTEXT_ITEM, Menu.NONE, "Unhide");
        } else {
            menu.add(0, Constants.HIDE_CONTEXT_ITEM, Menu.NONE, "Hide");
        }

        menu.add(0, Constants.DIALOG_VIEW_PROFILE, Menu.NONE,
                String.format(getResources().getString(R.string.user_profile), item.getAuthor()));

    } else if (isLoadMoreCommentsPosition(rowId)) {
        menu.add(0, Constants.DIALOG_GOTO_PARENT, Menu.NONE, "Go to parent");
    } else if (isHiddenCommentHeadPosition(rowId)) {
        menu.add(0, Constants.DIALOG_SHOW_COMMENT, Menu.NONE, "Show comment");
        menu.add(0, Constants.DIALOG_GOTO_PARENT, Menu.NONE, "Go to parent");
    } else {
        if (mSettings.getUsername() != null && mSettings.getUsername().equalsIgnoreCase(item.getAuthor())) {
            menu.add(0, Constants.DIALOG_EDIT, Menu.NONE, "Edit");
            menu.add(0, Constants.DIALOG_DELETE, Menu.NONE, "Delete");
        }
        menu.add(0, Constants.DIALOG_HIDE_COMMENT, Menu.NONE, "Hide comment");
        //          if (mSettings.isLoggedIn())
        //             menu.add(0, Constants.DIALOG_REPORT, Menu.NONE, "Report comment");
        menu.add(0, Constants.DIALOG_GOTO_PARENT, Menu.NONE, "Go to parent");
        menu.add(0, Constants.DIALOG_VIEW_PROFILE, Menu.NONE,
                String.format(getResources().getString(R.string.user_profile), item.getAuthor()));
    }
}

From source file:it.geosolutions.geocollect.android.core.mission.PendingMissionListFragment.java

/**
 * Create the Menu visible on longpress on items
 *//*w  ww .  j  a va2s . co m*/
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {

    // if this item is editable or uploadable, offer the possibility to "reset" the state -> delete its "sop" entry
    if (v.getId() == getListView().getId()) {
        ListView lv = (ListView) v;

        final MissionTemplate template = MissionUtils.getDefaultTemplate(getActivity());
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        MissionFeature feature = (MissionFeature) lv.getItemAtPosition(info.position);

        // identify edited/uploadable
        if (feature.editing
                || (feature.typeName != null && feature.typeName.endsWith(MissionTemplate.NEW_NOTICE_SUFFIX))) {
            // create a dialog to let the user clear this surveys data
            String title = getString(R.string.survey);
            if (feature.properties != null && feature.properties.get(template.nameField) != null) {
                title = (String) feature.properties.get(template.nameField);
            }
            if (title != null) {
                menu.setHeaderTitle(title);
            }

            menu.add(0, RESET_MISSION_FEATURE_ID, 0, getString(R.string.menu_clear_survey));

        }
    }
}

From source file:cgeo.geocaching.cgeocaches.java

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

    AdapterContextMenuInfo adapterInfo = null;
    try {/*from w  w  w.  j a v a2  s  .co  m*/
        adapterInfo = (AdapterContextMenuInfo) info;
    } catch (Exception e) {
        Log.w("cgeocaches.onCreateContextMenu", e);
    }

    if (adapterInfo == null || adapterInfo.position >= adapter.getCount()) {
        return;
    }
    final Geocache cache = adapter.getItem(adapterInfo.position);

    menu.setHeaderTitle(StringUtils.defaultIfBlank(cache.getName(), cache.getGeocode()));

    contextMenuGeocode = cache.getGeocode();

    if (cache.getCoords() != null) {
        menu.add(0, MENU_DEFAULT_NAVIGATION, 0,
                NavigationAppFactory.getDefaultNavigationApplication().getName());
        menu.add(1, MENU_NAVIGATION, 0, res.getString(R.string.cache_menu_navigate))
                .setIcon(R.drawable.ic_menu_mapmode);
        LoggingUI.addMenuItems(this, menu, cache);
        menu.add(0, MENU_CACHE_DETAILS, 0, res.getString(R.string.cache_menu_details));
    }
    if (cache.isOffline()) {
        menu.add(0, MENU_DROP_CACHE, 0, res.getString(R.string.cache_offline_drop));
        menu.add(0, MENU_MOVE_TO_LIST, 0, res.getString(R.string.cache_menu_move_list));
        menu.add(0, MENU_EXPORT, 0, res.getString(R.string.export));
        menu.add(0, MENU_REFRESH, 0, res.getString(R.string.cache_menu_refresh));
    } else {
        menu.add(0, MENU_STORE_CACHE, 0, res.getString(R.string.cache_offline_store));
    }
}

From source file:org.transdroid.gui.TorrentsFragment.java

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

    // Add the pause/resume/stop/start options
    Torrent selected = (Torrent) getTorrentListAdapter().getItem((int) ((AdapterContextMenuInfo) menuInfo).id);
    if (selected.canPause()) {
        menu.add(0, MENU_PAUSE_ID, 0, R.string.menu_pause);
    } else if (selected.canResume()) {
        menu.add(0, MENU_RESUME_ID, 0, R.string.menu_resume);
    }//from   w w w .  j a  v a2 s  .c o m
    if (daemon != null && Daemon.supportsStoppingStarting(daemon.getType())) {
        if (selected.canStop()) {
            menu.add(0, MENU_STOP_ID, 0, R.string.menu_stop);
        } else if (selected.canStart()) {
            menu.add(0, MENU_START_ID, 0, R.string.menu_start);
        }
    }
    if (daemon != null && Daemon.supportsForcedStarting(daemon.getType()) && selected.canStart()) {
        menu.add(0, MENU_FORCESTART_ID, 0, R.string.menu_forcestart);
    }

    // Add the remove options
    menu.add(0, MENU_REMOVE_ID, 0, R.string.menu_remove);
    if (daemon != null && Daemon.supportsRemoveWithData(daemon.getType())) {
        menu.add(0, MENU_REMOVE_DATA_ID, 0, R.string.menu_remove_data);
    }

    // Add the 'set label' option
    if (daemon != null && Daemon.supportsSetLabel(daemon.getType())) {
        menu.add(0, MENU_SETLABEL_ID, 0, R.string.menu_setlabel);
    }

    // Add the 'set download location' option
    if (daemon != null && Daemon.supportsSetDownloadLocation(daemon.getType())) {
        menu.add(0, MENU_SETDOWNLOADLOCATION_ID, 0, R.string.menu_setdownloadlocation);
    }

}

From source file:info.guardianproject.otr.app.im.app.NewChatActivity.java

/** Show the context menu on a history item. */
@Override//  w  w w. jav  a2  s  .  c  o  m
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {

    ChatView chatView = getCurrentChatView();

    if (chatView != null) {
        AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        mMessageContextMenuHandler.mPosition = info.position;
        Cursor cursor = chatView.getMessageAtPosition(info.position);
        int type = cursor.getInt(cursor.getColumnIndexOrThrow(Imps.Messages.TYPE));
        if (type == Imps.MessageType.OUTGOING) {
            android.view.MenuItem mi = menu.add(0, MENU_RESEND, 0, R.string.menu_resend);

            mi.setOnMenuItemClickListener(mMessageContextMenuHandler);

        }

    }
}

From source file:info.guardianproject.otr.app.im.app.ContactListActivity.java

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    boolean chatSelected = false;
    boolean contactSelected = false;
    Cursor contactCursor;/* w w  w . j a v  a 2s.  co m*/
    if (mIsFiltering) {
        AdapterView.AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
        mContextMenuHandler.mPosition = info.position;
        contactSelected = true;
        contactCursor = mFilterView.getContactAtPosition(info.position);
    } else {
        ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuInfo;
        mContextMenuHandler.mPosition = info.packedPosition;
        contactSelected = mContactListView.isContactAtPosition(info.packedPosition);
        contactCursor = mContactListView.getContactAtPosition(info.packedPosition);
    }

    boolean allowBlock = true;
    if (contactCursor != null) {
        //XXX HACK: Yahoo! doesn't allow to block a friend. We can only block a temporary contact.
        ProviderDef provider = mApp.getProvider(mProviderId);
        if (Imps.ProviderNames.YAHOO.equals(provider.mName)) {
            int type = contactCursor.getInt(contactCursor.getColumnIndexOrThrow(Imps.Contacts.TYPE));
            allowBlock = (type == Imps.Contacts.TYPE_TEMPORARY);
        }

        int nickNameIndex = contactCursor.getColumnIndexOrThrow(Imps.Contacts.NICKNAME);

        menu.setHeaderTitle(contactCursor.getString(nickNameIndex));
    }

    BrandingResources brandingRes = mApp.getBrandingResource(mProviderId);
    String menu_end_conversation = brandingRes.getString(BrandingResourceIDs.STRING_MENU_END_CHAT);
    String menu_view_profile = brandingRes.getString(BrandingResourceIDs.STRING_MENU_VIEW_PROFILE);
    String menu_block_contact = brandingRes.getString(BrandingResourceIDs.STRING_MENU_BLOCK_CONTACT);
    String menu_start_conversation = brandingRes.getString(BrandingResourceIDs.STRING_MENU_START_CHAT);
    String menu_delete_contact = brandingRes.getString(BrandingResourceIDs.STRING_MENU_DELETE_CONTACT);

    if (chatSelected) {
        menu.add(0, MENU_END_CONVERSATION, 0, menu_end_conversation)
                .setOnMenuItemClickListener(mContextMenuHandler);
        menu.add(0, MENU_VIEW_PROFILE, 0, menu_view_profile).setIcon(R.drawable.ic_menu_my_profile)
                .setOnMenuItemClickListener(mContextMenuHandler);
        if (allowBlock) {
            menu.add(0, MENU_BLOCK_CONTACT, 0, menu_block_contact)
                    .setOnMenuItemClickListener(mContextMenuHandler);
        }
    } else if (contactSelected) {
        menu.add(0, MENU_START_CONVERSATION, 0, menu_start_conversation)
                .setOnMenuItemClickListener(mContextMenuHandler);
        menu.add(0, MENU_VIEW_PROFILE, 0, menu_view_profile).setIcon(R.drawable.ic_menu_view_profile)
                .setOnMenuItemClickListener(mContextMenuHandler);
        if (allowBlock) {
            menu.add(0, MENU_BLOCK_CONTACT, 0, menu_block_contact)
                    .setOnMenuItemClickListener(mContextMenuHandler);
        }
        menu.add(0, MENU_DELETE_CONTACT, 0, menu_delete_contact).setIcon(android.R.drawable.ic_menu_delete)
                .setOnMenuItemClickListener(mContextMenuHandler);
    }
}

From source file:com.zen.androidhtmleditor.AHEActivity.java

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

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);

    String currentServers = settings.getString("Accounts", "");
    if (currentServers.equals("")) {
        menu.add(0, v.getId(), 0, "No Accounts");
    } else {/*w w  w.  j  ava 2s. c o m*/
        Gson gson = new Gson();
        SearchResponse response = gson.fromJson(currentServers, SearchResponse.class);
        List<Result> results = response.data;
        int menuCount = 0;
        for (Result l : results) {
            String accName = l.serverName;
            if (l.accountName != null && l.accountName.length() > 0) {
                accName = l.accountName;
            }
            menu.add(menuCount, v.getId(), menuCount, accName);
            menuCount++;
        }

    }
    //menu.add(0, v.getId(), 0, "zenstudio.com.au");  
    //menu.add(0, v.getId(), 0, "Action 2");  
}

From source file:com.andrew.apollo.ui.fragments.profile.ApolloFragment.java

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

    // Get the position of the selected item
    final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
    int mSelectedPosition = info.position - mAdapter.getOffset();

    // Create a new song
    mItem = mAdapter.getItem(mSelectedPosition);

    // TODO: Remove these mutable properties, parametrize the onMenuEvent handlers.
    if (mItem instanceof Song) {
        Song mSong = (Song) mItem;/*from w w w .j  a va2  s . c o  m*/
        mSelectedId = mSong.mSongId;
        mSongName = mSong.mSongName;
        mAlbumName = mSong.mAlbumName;
        mArtistName = mSong.mArtistName;
        mSongList = null;
    } else if (mItem instanceof Album) {
        Album mAlbum = (Album) mItem;
        mSelectedId = mAlbum.mAlbumId;
        mSongName = null;
        mAlbumName = mAlbum.mAlbumName;
        mArtistName = mAlbum.mArtistName;
        mSongList = MusicUtils.getSongListForAlbum(getActivity(), mAlbum.mAlbumId);
    } else if (mItem instanceof Artist) {
        Artist mArtist = (Artist) mItem;
        mSelectedId = mArtist.mArtistId;
        mSongName = null;
        mArtistName = mArtist.mArtistName;
        mSongList = MusicUtils.getSongListForArtist(getActivity(), mArtist.mArtistId);
    } else if (mItem instanceof Genre) {
        Genre mGenre = (Genre) mItem;
        mSelectedId = mGenre.mGenreId;
        mSongList = MusicUtils.getSongListForGenre(getActivity(), mGenre.mGenreId);
    } else if (mItem instanceof Playlist) {
        Playlist mPlaylist = (Playlist) mItem;
        mSelectedId = mPlaylist.mPlaylistId;
        if (mSelectedId == PlaylistLoader.FAVORITE_PLAYLIST_ID) {
            mSongList = MusicUtils.getSongListForFavorites(getActivity());
        } else if (mSelectedId == PlaylistLoader.LAST_ADDED_PLAYLIST_ID) {
            mSongList = MusicUtils.getSongListForLastAdded(getActivity());
        } else {
            mSongList = MusicUtils.getSongListForPlaylist(getActivity(), mPlaylist.mPlaylistId);
        }

    }

    // Play the selected songs
    menu.add(GROUP_ID, FragmentMenuItems.PLAY_SELECTION, Menu.NONE,
            getString(R.string.context_menu_play_selection));

    // Play the next song
    menu.add(GROUP_ID, FragmentMenuItems.PLAY_NEXT, Menu.NONE, getString(R.string.context_menu_play_next));

    // Add the song/album to the queue
    menu.add(GROUP_ID, FragmentMenuItems.ADD_TO_QUEUE, Menu.NONE, getString(R.string.add_to_queue));

    // Add the song to favorite's playlist
    menu.add(GROUP_ID, FragmentMenuItems.ADD_TO_FAVORITES, Menu.NONE, R.string.add_to_favorites);

    // Add the song/album to a playlist
    final SubMenu subMenu = menu.addSubMenu(GROUP_ID, FragmentMenuItems.ADD_TO_PLAYLIST, Menu.NONE,
            R.string.add_to_playlist);
    MusicUtils.makePlaylistMenu(getActivity(), GROUP_ID, subMenu, true);

    if (mItem instanceof Song) {
        menu.add(GROUP_ID, FragmentMenuItems.USE_AS_RINGTONE, Menu.NONE,
                getString(R.string.context_menu_use_as_ringtone));
    }

    // More by artist
    menu.add(GROUP_ID, FragmentMenuItems.MORE_BY_ARTIST, Menu.NONE,
            getString(R.string.context_menu_more_by_artist));

    // Delete the album
    menu.add(GROUP_ID, FragmentMenuItems.DELETE, Menu.NONE, getString(R.string.context_menu_delete));
}

From source file:com.kll.collect.android.activities.FormEntryActivity.java

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    Collect.getInstance().getActivityLogger().logInstanceAction(this, "onCreateContextMenu", "show");
    FormController formController = Collect.getInstance().getFormController();

    menu.add(0, v.getId(), 0, getString(R.string.clear_answer));
    if (formController.indexContainsRepeatableGroup()) {
        menu.add(0, DELETE_REPEAT, 0, getString(R.string.delete_repeat));
    }/*from w w  w .  ja v  a2 s.co  m*/
    menu.setHeaderTitle(getString(R.string.edit_prompt));
}

From source file:com.oakesville.mythling.MediaActivity.java

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    if (v == getListView()) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        Listable listable = (Listable) getListView().getItemAtPosition(info.position);
        if (listable instanceof Item) {
            Item item = (Item) listable;
            menu.setHeaderTitle(item.getDialogTitle());
            if (isSplitView()) {
                getListView().performItemClick(getListView().getChildAt(info.position), info.position,
                        getListView().getAdapter().getItemId(info.position));
            } else {
                getListView().setItemChecked(info.position, true);
            }//ww w  . ja  v  a 2  s .c om
            SparseArray<String> menuItems = getLongClickMenuItems(item);
            for (int i = 0; i < menuItems.size(); i++) {
                int id = menuItems.keyAt(i);
                menu.add(MEDIA_ACTIVITY_CONTEXT_MENU_GROUP_ID, id, id, menuItems.get(id));
            }
        }
    }
}