@Override public boolean onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); hackForSubmenus = menu; if (progress != null && (actionBarCompat == null || actionBarCompat.hasMenuButton())) return false; // not safe/useful until game is loaded MenuItem item; item = menu.findItem(R.id.solve); item.setEnabled(solveEnabled); if (actionBarCompat != null) item.setVisible(solveEnabled); MenuItem undoItem = menu.findItem(R.id.undo), redoItem = menu.findItem(R.id.redo); undoItem.setEnabled(undoEnabled); redoItem.setEnabled(redoEnabled); undoItem.setIcon( undoEnabled ? R.drawable.sym_keyboard_undo : R.drawable.sym_keyboard_undo_disabled); redoItem.setIcon( redoEnabled ? R.drawable.sym_keyboard_redo : R.drawable.sym_keyboard_redo_disabled); item = menu.findItem(R.id.type); item.setEnabled(!gameTypes.isEmpty() || customVisible); if (actionBarCompat != null) item.setVisible(item.isEnabled()); typeMenu = item.getSubMenu(); for (Integer i : gameTypes.keySet()) { if (menu.findItem(i) == null) typeMenu.add(R.id.typeGroup, i, Menu.NONE, gameTypes.get(i)); } MenuItem customItem = menu.findItem(R.id.custom); customItem.setVisible(customVisible); typeMenu.setGroupCheckable(R.id.typeGroup, true, true); if (currentType < 0) customItem.setChecked(true); else if (currentType < gameTypes.size()) menu.findItem((Integer) gameTypes.keySet().toArray()[currentType]).setChecked(true); menu.findItem(R.id.thisgame) .setTitle( MessageFormat.format(getString(R.string.help_on_game), new Object[] {this.getTitle()})); return true; }
/** * Handles clicks on the AppMenu popup. * * @param menuItem The menu item in the popup that was clicked. */ void onItemClick(MenuItem menuItem) { if (menuItem.isEnabled()) { if (menuItem.getItemId() == R.id.update_menu_id) { UpdateMenuItemHelper.getInstance().setMenuItemClicked(); } dismiss(); mHandler.onOptionsItemSelected(menuItem); } }
private View addMenuButton(final MenuItem item) { final ImageButton actionButton = new ImageButton(mContext, null, R.attr.actionBarItemStyle); final LayoutParams params = new LayoutParams( (int) getResources().getDimension(R.dimen.actionbar_button_width), ViewGroup.LayoutParams.MATCH_PARENT); params.weight = 1; actionButton.setLayoutParams(params); actionButton.setImageDrawable(item.getIcon()); actionButton.setScaleType(ScaleType.CENTER); actionButton.setContentDescription(item.getTitle()); actionButton.setEnabled(item.isEnabled()); actionButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(final View view) { if (!item.isEnabled()) return; if (item.hasSubMenu()) { mPopupMenu = PopupMenu.getInstance(mContext, view); mPopupMenu.setOnMenuItemClickListener(MenuBar.this); mPopupMenu.setMenu(item.getSubMenu()); mPopupMenu.show(); } else { if (mItemClickListener != null) { mItemClickListener.onMenuItemClick(item); } } } }); actionButton.setOnLongClickListener( new View.OnLongClickListener() { @Override public boolean onLongClick(final View v) { if (item.getItemId() == android.R.id.home) return false; final Toast t = Toast.makeText(mContext, item.getTitle(), Toast.LENGTH_SHORT); final int[] screenPos = new int[2]; v.getLocationOnScreen(screenPos); final int height = v.getHeight(); t.setGravity( Gravity.TOP | Gravity.LEFT, screenPos[0], (int) (screenPos[1] - height * 1.5)); t.show(); return true; } }); addView(actionButton); return actionButton; }
private void setItemEnabled(Menu menu, int menuId, boolean isEnabled) { final MenuItem item = menu.findItem(menuId); if (item == null || item.isEnabled() == isEnabled) return; item.setEnabled(isEnabled); if (item.getIcon() != null) { // must mutate the drawable to avoid affecting other instances of it Drawable icon = item.getIcon().mutate(); icon.setAlpha(isEnabled ? 255 : 128); item.setIcon(icon); } }
/** Returns the visible and enabled menu items in the specified menu. This method is recursive. */ private List<MenuItem> getVisibleAndEnabledMenuItems(Menu menu) { List<MenuItem> menuItems = new ArrayList<MenuItem>(); for (int i = 0; (menu != null) && (i < menu.size()); i++) { MenuItem menuItem = menu.getItem(i); if (menuItem.isVisible() && menuItem.isEnabled()) { Menu subMenu = menuItem.getSubMenu(); if (subMenu != null) { menuItems.addAll(getVisibleAndEnabledMenuItems(subMenu)); } else { menuItems.add(menuItem); } } } return menuItems; }
@Override public boolean isEnabled() { return mNativeItem.isEnabled(); }
/** * Changes the menu item's icon to reflect enabled or disabled state. * * @param menuItem The menu item which icon is to be set */ public static void setMenuIconState(MenuItem menuItem) { menuItem.getIcon().setAlpha(menuItem.isEnabled() ? 255 : 64); }
@Override public boolean isEnabled() { return mMenuItem.isEnabled(); }
/** * Reorders the specified {@link Menu}, taking into account the user's Archive/Delete preference. */ public static void reorderMenu( final Context context, final Account account, final Menu menu, final int maxItems) { final String removalAction = MailPrefs.get(context) .getRemovalAction(account.supportsCapability(AccountCapabilities.ARCHIVE)); final boolean showArchive = MailPrefs.RemovalActions.ARCHIVE.equals(removalAction) || MailPrefs.RemovalActions.ARCHIVE_AND_DELETE.equals(removalAction); final boolean showDelete = MailPrefs.RemovalActions.DELETE.equals(removalAction) || MailPrefs.RemovalActions.ARCHIVE_AND_DELETE.equals(removalAction); // Do a first pass to extract necessary information on what is safe to display boolean archiveVisibleEnabled = false; boolean deleteVisibleEnabled = false; for (int i = 0; i < menu.size(); i++) { final MenuItem menuItem = menu.getItem(i); final int itemId = menuItem.getItemId(); final boolean visible = menuItem.isVisible(); final boolean enabled = menuItem.isEnabled(); if (itemId == R.id.archive || itemId == R.id.remove_folder) { archiveVisibleEnabled |= (visible & enabled); } else if (itemId == R.id.delete || itemId == R.id.discard_drafts) { deleteVisibleEnabled |= (visible & enabled); } } int actionItems = 0; for (int i = 0; i < menu.size(); i++) { final MenuItem menuItem = menu.getItem(i); final int itemId = menuItem.getItemId(); // We only want to promote it if it's visible and has an icon if (menuItem.isVisible() && menuItem.getIcon() != null) { if (itemId == R.id.archive || itemId == R.id.remove_folder) { /* * If this is disabled, and we want to show both archive and delete, we will * hide archive (rather than showing it disabled), and take up one of our * spaces. If we only want to show archive, we'll hide it, but not take up * a space. */ if (!menuItem.isEnabled() && showArchive) { menuItem.setVisible(false); if (showDelete) { actionItems++; } } else { /* * We show this if the following are all true: * 1. The user wants to display archive, or delete is not visible * 2. We have room for it */ if ((showArchive || !deleteVisibleEnabled) && actionItems < maxItems) { menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); actionItems++; } } } else if (itemId == R.id.delete || itemId == R.id.discard_drafts) { /* * We show this if the following are all true: * 1. The user wants to display delete, or archive is not visible * 2. We have room for it */ if ((showDelete || !archiveVisibleEnabled) && actionItems < maxItems) { menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); actionItems++; } } else if (itemId == R.id.change_folders) { final boolean showChangeFolder = account.supportsCapability(AccountCapabilities.MULTIPLE_FOLDERS_PER_CONV); menuItem.setVisible(showChangeFolder); if (showChangeFolder && actionItems < maxItems) { menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); actionItems++; } } else if (itemId == R.id.search) { menuItem.setShowAsAction( MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); actionItems++; } else { if (actionItems < maxItems) { menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); actionItems++; } } } } }