/** * Invokes the item by calling various listeners or callbacks. * * @return true if the invocation was handled, false otherwise */ public boolean invoke() { if (mClickListener != null && mClickListener.onMenuItemClick(this)) { return true; } if (mMenu.dispatchMenuItemSelected(mMenu.getRootMenu(), this)) { return true; } if (mItemCallback != null) { mItemCallback.run(); return true; } if (mIntent != null) { try { mMenu.getContext().startActivity(mIntent); return true; } catch (ActivityNotFoundException e) { Log.e(TAG, "Can't find activity to handle intent; ignoring", e); } } if (mActionProvider != null && mActionProvider.onPerformDefaultAction()) { return true; } return false; }
/** * Instantiates this menu item. * * @param menu * @param group Item ordering grouping control. The item will be added after all other items whose * order is <= this number, and before any that are larger than it. This can also be used to * define groups of items for batch state changes. Normally use 0. * @param id Unique item ID. Use 0 if you do not need a unique ID. * @param categoryOrder The ordering for this item. * @param title The text to display for the item. */ MenuItemImpl( MenuBuilder menu, int group, int id, int categoryOrder, int ordering, CharSequence title, int showAsAction) { String lang = menu.getContext().getResources().getConfiguration().locale.toString(); if (sPrependShortcutLabel == null || !lang.equals(sLanguage)) { sLanguage = lang; // This is instantiated from the UI thread, so no chance of sync issues sPrependShortcutLabel = menu.getContext() .getResources() .getString(com.android.internal.R.string.prepend_shortcut_label); sEnterShortcutLabel = menu.getContext() .getResources() .getString(com.android.internal.R.string.menu_enter_shortcut_label); sDeleteShortcutLabel = menu.getContext() .getResources() .getString(com.android.internal.R.string.menu_delete_shortcut_label); sSpaceShortcutLabel = menu.getContext() .getResources() .getString(com.android.internal.R.string.menu_space_shortcut_label); } mMenu = menu; mId = id; mGroup = group; mCategoryOrder = categoryOrder; mOrdering = ordering; mTitle = title; mShowAsAction = showAsAction; }
public Drawable getIcon() { if (mIconDrawable != null) { return mIconDrawable; } if (mIconResId != NO_ICON) { Drawable icon = mMenu.getContext().getDrawable(mIconResId); mIconResId = NO_ICON; mIconDrawable = icon; return icon; } return null; }
public MenuItem setActionView(int resId) { final Context context = mMenu.getContext(); final LayoutInflater inflater = LayoutInflater.from(context); setActionView(inflater.inflate(resId, new LinearLayout(context), false)); return this; }
public MenuItem setTitle(int title) { return setTitle(mMenu.getContext().getString(title)); }