List of usage examples for android.view ContextMenu add
public MenuItem add(int groupId, int itemId, int order, CharSequence title);
From source file:edu.chalmers.dat255.audiobookplayer.view.BookshelfFragment.java
@SuppressWarnings("rawtypes") @Override/*from w ww .j a va2 s .c om*/ public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { // check that the menuInfo is of the correct type // this method is allowed to have quite a high cyclomatic complexity as // it would otherwise cause code duplication if (menuInfo instanceof ExpandableListContextMenuInfo && adapter != null) { ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuInfo; // get the provided book position int bookIndex = ExpandableListView.getPackedPositionGroup(info.packedPosition); // trackIndex will be -1 if group is clicked int trackIndex = ExpandableListView.getPackedPositionChild(info.packedPosition); // get the type of the context menu int type = ExpandableListView.getPackedPositionType(info.packedPosition); // create an empty array to prevent trying to loop over an // uninitialized variable IContextMenuItem[] menuItems = new IContextMenuItem[0]; String title = ""; // fill the context menu with the correct items if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { // get all menu items from the child context menu menuItems = ChildContextMenuItem.values(); // set the context menu's title to that of the value of the // child title = adapter.getChild(bookIndex, trackIndex); } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) { // get all menu items from the group context menu menuItems = GroupContextMenuItem.values(); // set the context menu's title to that of the value of the book title = adapter.getGroup(bookIndex); } // set the title menu.setHeaderTitle(title); // populate the context menu with items in the order they were // declared in the enum declaration. for (IContextMenuItem item : menuItems) { // as this only loops when menuItems is of either of type // GroupContextMenuItem[] or ChildContextMenuItem[], Enum can be // used as a raw type menu.add(Menu.NONE, ((Enum) item).ordinal(), ((Enum) item).ordinal(), item.getText()); } } }
From source file:cm.aptoide.pt.MainActivity.java
@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { switch (v.getId()) { case R.id.available_list: Integer tag = (Integer) ((AdapterContextMenuInfo) menuInfo).targetView.getTag(); if (tag != null && tag == 1) { menu.add(0, 1, 0, R.string.menu_context_reparse); }/*w ww. ja v a 2 s .co m*/ menu.add(0, 0, 0, R.string.menu_context_remove); break; case R.id.updates_list: Log.d("onCreateContextMenu", "onCreateContextMenu"); menu.add(0, (int) ((AdapterContextMenuInfo) menuInfo).id, 0, mContext.getString(R.string.exclude_update)) .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { System.out.println(item.getItemId()); db.addToExcludeUpdate(item.getItemId()); updatesLoader.forceLoad(); return false; } }); break; } }
From source file:de.tum.in.tumcampus.auxiliary.calendar.DayView.java
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) { MenuItem item;//from w w w . ja v a 2s .co m // If the trackball is held down, then the context menu pops up and // we never get onKeyUp() for the long-press. So check for it here // and change the selection to the long-press state. /*if (mSelectionMode != SELECTION_LONGPRESS) { mSelectionMode = SELECTION_LONGPRESS; invalidate(); }*/ final long startMillis = getSelectedTimeInMillis(); int flags = DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_CAP_NOON_MIDNIGHT | DateUtils.FORMAT_SHOW_WEEKDAY; final String title = DayUtils.formatDateRange(mContext, startMillis, startMillis, flags); menu.setHeaderTitle(title); int numSelectedEvents = mSelectedEvents.size(); if (mNumDays == 1) { // Day view. // If there is a selected event, then allow it to be viewed and // edited. if (numSelectedEvents >= 1) { item = menu.add(0, MENU_EVENT_VIEW, 0, "View event"); item.setOnMenuItemClickListener(mContextMenuHandler); item.setIcon(android.R.drawable.ic_menu_info_details); } } else { // Week view. // If there is a selected event, then allow it to be viewed and // edited. if (numSelectedEvents >= 1) { item = menu.add(0, MENU_EVENT_VIEW, 0, "View event"); item.setOnMenuItemClickListener(mContextMenuHandler); item.setIcon(android.R.drawable.ic_menu_info_details); } item = menu.add(0, MENU_DAY, 0, "Show day"); item.setOnMenuItemClickListener(mContextMenuHandler); item.setIcon(android.R.drawable.ic_menu_day); item.setAlphabeticShortcut('d'); } }
From source file:org.openintents.shopping.ui.ShoppingActivity.java
/** * Hook up buttons, lists, and edittext with functionality. *///from www . j ava 2 s. co m private void createView() { // Temp-create either Spinner or List based upon the Display createList(); mAddPanel = findViewById(R.id.add_panel); mEditText = (AutoCompleteTextView) findViewById(R.id.autocomplete_add_item); fillAutoCompleteTextViewAdapter(mEditText); mEditText.setThreshold(1); mEditText.setOnKeyListener(new OnKeyListener() { private int mLastKeyAction = KeyEvent.ACTION_UP; public boolean onKey(View v, int keyCode, KeyEvent key) { // Shortcut: Instead of pressing the button, // one can also press the "Enter" key. if (debug) { Log.i(TAG, "Key action: " + key.getAction()); } if (debug) { Log.i(TAG, "Key code: " + keyCode); } if (keyCode == KeyEvent.KEYCODE_ENTER) { if (mEditText.isPopupShowing()) { mEditText.performCompletion(); } // long key press might cause call of duplicate onKey events // with ACTION_DOWN // this would result in inserting an item and showing the // pick list if (key.getAction() == KeyEvent.ACTION_DOWN && mLastKeyAction == KeyEvent.ACTION_UP) { insertNewItem(); } mLastKeyAction = key.getAction(); return true; } return false; } }); mEditText.addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable s) { if (mItemsView.mMode == MODE_ADD_ITEMS) { // small optimization: Only care about updating // the button label on each key pressed if we // are in "add items" mode. updateButton(); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } }); mButton = (Button) findViewById(R.id.button_add_item); mButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { insertNewItem(); } }); mButton.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { if (PreferenceActivity.getAddForBarcode(getApplicationContext()) == false) { if (debug) { Log.v(TAG, "barcode scanner on add button long click disabled"); } return false; } Intent intent = new Intent(); intent.setData(mListUri); intent.setClassName("org.openintents.barcodescanner", "org.openintents.barcodescanner.BarcodeScanner"); intent.setAction(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_ALTERNATIVE); try { startActivityForResult(intent, REQUEST_CODE_CATEGORY_ALTERNATIVE); } catch (ActivityNotFoundException e) { if (debug) { Log.v(TAG, "barcode scanner not found"); } showDialog(DIALOG_GET_FROM_MARKET); return false; } // Instead of calling the class of barcode // scanner directly, a more generic approach would // be to use a general activity picker. // // TODO: Implement onActivityResult. // Problem: User has to pick activity every time. // Choice should be storeable in Stettings. // Intent intent = new Intent(Intent.ACTION_GET_CONTENT); // intent.setData(mListUri); // intent.addCategory(Intent.CATEGORY_ALTERNATIVE); // // Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY); // pickIntent.putExtra(Intent.EXTRA_INTENT, intent); // pickIntent.putExtra(Intent.EXTRA_TITLE, // getText(R.string.title_select_item_from)); // try { // startActivityForResult(pickIntent, // REQUEST_CODE_CATEGORY_ALTERNATIVE); // } catch (ActivityNotFoundException e) { // Log.v(TAG, "barcode scanner not found"); // return false; // } return true; } }); mLayoutParamsItems = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); mToastBar = (ActionableToastBar) findViewById(R.id.toast_bar); mItemsView = (ShoppingItemsView) findViewById(R.id.list_items); mItemsView.setThemedBackground(findViewById(R.id.background)); mItemsView.setCustomClickListener(this); mItemsView.setToastBar(mToastBar); mItemsView.initTotals(); mItemsView.setItemsCanFocus(true); mItemsView.setDragListener(new DragListener() { @Override public void drag(int from, int to) { if (debug) { Log.v("DRAG", "" + from + "/" + to); } } }); mItemsView.setDropListener(new DropListener() { @Override public void drop(int from, int to) { if (debug) { Log.v("DRAG", "" + from + "/" + to); } } }); mItemsView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int pos, long id) { Cursor c = (Cursor) parent.getItemAtPosition(pos); onCustomClick(c, pos, EditItemDialog.FieldType.ITEMNAME, v); // DO NOT CLOSE THIS CURSOR - IT IS A MANAGED ONE. // ---- c.close(); } }); mItemsView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() { public void onCreateContextMenu(ContextMenu contextmenu, View view, ContextMenuInfo info) { contextmenu.add(0, MENU_EDIT_ITEM, 0, R.string.menu_edit_item).setShortcut('1', 'e'); contextmenu.add(0, MENU_MARK_ITEM, 0, R.string.menu_mark_item).setShortcut('2', 'm'); contextmenu.add(0, MENU_ITEM_STORES, 0, R.string.menu_item_stores).setShortcut('3', 's'); contextmenu.add(0, MENU_REMOVE_ITEM_FROM_LIST, 0, R.string.menu_remove_item).setShortcut('4', 'r'); contextmenu.add(0, MENU_COPY_ITEM, 0, R.string.menu_copy_item).setShortcut('5', 'c'); contextmenu.add(0, MENU_DELETE_ITEM, 0, R.string.menu_delete_item).setShortcut('6', 'd'); contextmenu.add(0, MENU_MOVE_ITEM, 0, R.string.menu_move_item).setShortcut('7', 'l'); } }); }
From source file:com.nttec.everychan.ui.presentation.BoardFragment.java
@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); //? ? -//from w w w.j a v a 2 s. c o m if (v.getTag() != null && v.getTag() instanceof AttachmentModel) { lastContextMenuAttachment = v; AttachmentModel model = (AttachmentModel) v.getTag(); View tnView = v.findViewById(R.id.post_thumbnail_image); if (tnView != null && tnView.getTag() == Boolean.FALSE && !downloadThumbnails()) { menu.add(Menu.NONE, R.id.context_menu_thumb_load_thumb, 1, R.string.context_menu_show_thumbnail) .setOnMenuItemClickListener(contextMenuListener); } menu.add(Menu.NONE, R.id.context_menu_thumb_download, 2, R.string.context_menu_download_file); menu.add(Menu.NONE, R.id.context_menu_thumb_copy_url, 3, R.string.context_menu_copy_url); menu.add(Menu.NONE, R.id.context_menu_thumb_attachment_info, 4, R.string.context_menu_attachment_info); menu.add(Menu.NONE, R.id.context_menu_thumb_reverse_search, 5, R.string.context_menu_reverse_search); for (int id : new int[] { R.id.context_menu_thumb_download, R.id.context_menu_thumb_copy_url, R.id.context_menu_thumb_attachment_info, R.id.context_menu_thumb_reverse_search }) { menu.findItem(id).setOnMenuItemClickListener(contextMenuListener); } switch (model.type) { case AttachmentModel.TYPE_AUDIO: case AttachmentModel.TYPE_VIDEO: case AttachmentModel.TYPE_OTHER_FILE: menu.findItem(R.id.context_menu_thumb_reverse_search).setVisible(false); break; case AttachmentModel.TYPE_OTHER_NOTFILE: menu.findItem(R.id.context_menu_thumb_reverse_search).setVisible(false); menu.findItem(R.id.context_menu_thumb_download).setVisible(false); break; } if (tabModel.type == TabModel.TYPE_LOCAL) { menu.findItem(R.id.context_menu_thumb_download).setVisible(false); } return; } if (menu.findItem(R.id.context_menu_thumb_copy_url) != null) return; //? ? ? boolean isList = true; lastContextMenuPosition = -1; final PresentationItemModel model; if (v.getId() == android.R.id.list) { AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; model = adapter.getItem(info.position); if (model.hidden) { return; } } else { if (v.getTag() != null && v.getTag() instanceof PostsListAdapter.PostViewTag) { PostsListAdapter.PostViewTag tag = (PostsListAdapter.PostViewTag) v.getTag(); if (!tag.isPopupDialog) return; isList = false; lastContextMenuPosition = tag.position; model = adapter.getItem(lastContextMenuPosition); } else { return; } } if (pageType == TYPE_POSTSLIST) { menu.add(Menu.NONE, R.id.context_menu_reply, 1, R.string.context_menu_reply); menu.add(Menu.NONE, R.id.context_menu_reply_with_quote, 2, R.string.context_menu_reply_with_quote); menu.add(Menu.NONE, R.id.context_menu_select_text, 3, Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && isList ? R.string.context_menu_select_text : R.string.context_menu_copy_text); menu.add(Menu.NONE, R.id.context_menu_share, 4, R.string.context_menu_share); menu.add(Menu.NONE, R.id.context_menu_hide, 5, R.string.context_menu_hide_post); menu.add(Menu.NONE, R.id.context_menu_delete, 6, R.string.context_menu_delete); menu.add(Menu.NONE, R.id.context_menu_report, 7, R.string.context_menu_report); menu.add(Menu.NONE, R.id.context_menu_subscribe, 8, R.string.context_menu_subscribe); if (!isList) { for (int id : new int[] { R.id.context_menu_reply, R.id.context_menu_reply_with_quote, R.id.context_menu_select_text, R.id.context_menu_share, R.id.context_menu_hide, R.id.context_menu_delete, R.id.context_menu_report, R.id.context_menu_subscribe }) { menu.findItem(id).setOnMenuItemClickListener(contextMenuListener); } } if (presentationModel.source.boardModel.readonlyBoard || tabModel.type == TabModel.TYPE_LOCAL) { menu.findItem(R.id.context_menu_reply).setVisible(false); menu.findItem(R.id.context_menu_reply_with_quote).setVisible(false); } if (model.isDeleted || (!presentationModel.source.boardModel.allowDeletePosts && (!presentationModel.source.boardModel.allowDeleteFiles || model.sourceModel.attachments == null || model.sourceModel.attachments.length == 0)) || tabModel.type == TabModel.TYPE_LOCAL) { menu.findItem(R.id.context_menu_delete).setVisible(false); } if (model.isDeleted || presentationModel.source.boardModel.allowReport == BoardModel.REPORT_NOT_ALLOWED || tabModel.type == TabModel.TYPE_LOCAL) { menu.findItem(R.id.context_menu_report).setVisible(false); } if (settings.isSubscriptionsEnabled()) { if (subscriptions.hasSubscription(chan.getChanName(), presentationModel.source.boardModel.boardName, presentationModel.source.pageModel.threadNumber, model.sourceModel.number)) { menu.findItem(R.id.context_menu_subscribe).setTitle(R.string.context_menu_unsubscribe); } } else { menu.findItem(R.id.context_menu_subscribe).setVisible(false); } } else if (pageType == TYPE_THREADSLIST && isList) { menu.add(Menu.NONE, R.id.context_menu_open_in_new_tab, 1, R.string.context_menu_open_in_new_tab); menu.add(Menu.NONE, R.id.context_menu_thread_preview, 2, R.string.context_menu_thread_preview); menu.add(Menu.NONE, R.id.context_menu_reply_no_reading, 3, R.string.context_menu_reply_no_reading); menu.add(Menu.NONE, R.id.context_menu_hide, 4, R.string.context_menu_hide_thread); if (presentationModel.source.boardModel.readonlyBoard || tabModel.type == TabModel.TYPE_LOCAL) { menu.findItem(R.id.context_menu_reply_no_reading).setVisible(false); } } }
From source file:com.android.mms.ui.ComposeMessageActivity.java
private final void addCallAndContactMenuItems(ContextMenu menu, MsgListMenuClickListener l, MessageItem msgItem) {//from w w w .j av a2 s.co m if (TextUtils.isEmpty(msgItem.mBody)) { return; } SpannableString msg = new SpannableString(msgItem.mBody); Linkify.addLinks(msg, Linkify.ALL); ArrayList<String> uris = MessageUtils.extractUris(msg.getSpans(0, msg.length(), URLSpan.class)); // Remove any dupes so they don't get added to the menu multiple times HashSet<String> collapsedUris = new HashSet<String>(); for (String uri : uris) { collapsedUris.add(uri.toLowerCase()); } for (String uriString : collapsedUris) { String prefix = null; int sep = uriString.indexOf(":"); if (sep >= 0) { prefix = uriString.substring(0, sep); uriString = uriString.substring(sep + 1); } Uri contactUri = null; boolean knownPrefix = true; if ("mailto".equalsIgnoreCase(prefix)) { contactUri = getContactUriForEmail(uriString); } else if ("tel".equalsIgnoreCase(prefix)) { contactUri = getContactUriForPhoneNumber(uriString); } else { knownPrefix = false; } if (knownPrefix && contactUri == null) { Intent intent = ConversationList.createAddContactIntent(uriString); String addContactString = getString(R.string.menu_add_address_to_contacts, uriString); menu.add(0, MENU_ADD_ADDRESS_TO_CONTACTS, 0, addContactString).setOnMenuItemClickListener(l) .setIntent(intent); } } }
From source file:de.baumann.browser.Browser.java
@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); final WebView.HitTestResult result = mWebView.getHitTestResult(); final MenuItem.OnMenuItemClickListener handler = new MenuItem.OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { final String url = result.getExtra(); switch (item.getItemId()) { //Save image to external memory case ID_SAVE_IMAGE: { registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); try { if (url != null) { Uri source = Uri.parse(url); DownloadManager.Request request = new DownloadManager.Request(source); request.addRequestHeader("Cookie", CookieManager.getInstance().getCookie(url)); request.allowScanningByMediaScanner(); request.setNotificationVisibility( DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed! request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, helper_main.newFileName()); DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); dm.enqueue(request); Snackbar.make(mWebView, getString(R.string.context_saveImage_toast) + " " + helper_main.newFileName(), Snackbar.LENGTH_SHORT).show(); }/*from w w w .ja va 2 s. c o m*/ } catch (Exception e) { e.printStackTrace(); Snackbar.make(mWebView, R.string.toast_perm, Snackbar.LENGTH_SHORT).show(); } } break; case ID_SHARE_IMAGE: if (url != null) { shareString = helper_main.newFileName(); shareFile = helper_main.newFile(); try { Uri source = Uri.parse(url); DownloadManager.Request request = new DownloadManager.Request(source); request.addRequestHeader("Cookie", CookieManager.getInstance().getCookie(url)); request.allowScanningByMediaScanner(); request.setNotificationVisibility( DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed! request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, shareString); DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); dm.enqueue(request); Snackbar.make(mWebView, getString(R.string.context_saveImage_toast) + " " + helper_main.newFileName(), Snackbar.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); Snackbar.make(mWebView, R.string.toast_perm, Snackbar.LENGTH_SHORT).show(); } registerReceiver(onComplete2, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); } break; case ID_READ_LATER: if (url != null) { int domainInt = url.indexOf("//") + 2; final String domain = url.substring(domainInt, url.indexOf('/', domainInt)); try { final Database_ReadLater db = new Database_ReadLater(Browser.this); db.addBookmark(domain, url); db.close(); Snackbar.make(mWebView, R.string.readLater_added, Snackbar.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); } } break; case ID_COPY_LINK: if (url != null) { ClipboardManager clipboard = (ClipboardManager) Browser.this .getSystemService(Context.CLIPBOARD_SERVICE); clipboard.setPrimaryClip(ClipData.newPlainText("text", url)); Snackbar.make(mWebView, R.string.context_linkCopy_toast, Snackbar.LENGTH_SHORT).show(); } break; case ID_SHARE_LINK: if (url != null) { Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_TEXT, url); sendIntent.setType("text/plain"); Browser.this.startActivity(Intent.createChooser(sendIntent, getResources().getString(R.string.app_share_link))); } break; } return true; } }; if (result.getType() == WebView.HitTestResult.IMAGE_TYPE) { menu.add(0, ID_SAVE_IMAGE, 0, getString(R.string.context_saveImage)) .setOnMenuItemClickListener(handler); menu.add(0, ID_SHARE_IMAGE, 0, getString(R.string.context_shareImage)) .setOnMenuItemClickListener(handler); menu.add(0, ID_READ_LATER, 0, getString(R.string.context_readLater)) .setOnMenuItemClickListener(handler); } else if (result.getType() == WebView.HitTestResult.SRC_ANCHOR_TYPE) { menu.add(0, ID_COPY_LINK, 0, getString(R.string.menu_share_link_copy)) .setOnMenuItemClickListener(handler); menu.add(0, ID_SHARE_LINK, 0, getString(R.string.menu_share_link)).setOnMenuItemClickListener(handler); menu.add(0, ID_READ_LATER, 0, getString(R.string.context_readLater)) .setOnMenuItemClickListener(handler); } }