List of usage examples for android.view MenuItem setIcon
public MenuItem setIcon(@DrawableRes int iconRes);
From source file:com.lastsoft.plog.GamesFragment.java
@Override public boolean onOptionsItemSelected(MenuItem item) { // Get item selected and deal with it switch (item.getItemId()) { case android.R.id.home: //called when the up affordance/carat in actionbar is pressed mActivity.onBackPressed();/* ww w . jav a2s . c om*/ return true; case R.id.random_game: //select random bucket list game SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(mActivity); List<Game> theGames = mAdapter.getGames(); int min = 0; int max = theGames.size(); Random r = new Random(); int randomGame; if (!app_preferences.getBoolean("bucket_list_weight", false)) { randomGame = r.nextInt(max - min) + min; } else { //build weights int[] weightHash = new int[max]; int weightedMax = 0; for (int i = 1; i <= max; i++) { weightedMax = weightedMax + i; weightHash[i - 1] = weightedMax; } randomGame = r.nextInt(weightedMax - min) + min; for (int j = max - 1; j >= 0; j--) { if (randomGame < weightHash[j]) { //it may fall in this range //see if it's larger than the next one //if this one is zero, we found it if (j == 0) { randomGame = j; break; } else if (randomGame > weightHash[j - 1]) { randomGame = j; break; } else if (randomGame == weightHash[j - 1]) { randomGame = j - 1; break; } } } } Snackbar.make(mCoordinatorLayout, getString(R.string.random_game_to_play) + theGames.get(randomGame).gameName, Snackbar.LENGTH_LONG).show(); // Do not forget to show! return true; case R.id.sort: View menuItemView = mActivity.findViewById(R.id.sort); PopupMenu popup = new PopupMenu(mActivity, menuItemView); popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.sort_az: sortType = 0; mAdapter.updateData( mAdapter.generateGameList(mSearchQuery, playListType, sortType, currentYear)); mRecyclerView.scrollToPosition(0); if (mSearch != null) { mSearch.setHint(getString(R.string.filter) + mAdapter.getItemCount() + getString(R.string.filter_games)); } return true; case R.id.sort_za: sortType = 1; mAdapter.updateData( mAdapter.generateGameList(mSearchQuery, playListType, sortType, currentYear)); mRecyclerView.scrollToPosition(0); return true; case R.id.sort_plays_x0: sortType = 2; mAdapter.updateData( mAdapter.generateGameList(mSearchQuery, playListType, sortType, currentYear)); mRecyclerView.scrollToPosition(0); return true; case R.id.sort_plays_0x: sortType = 3; mAdapter.updateData( mAdapter.generateGameList(mSearchQuery, playListType, sortType, currentYear)); mRecyclerView.scrollToPosition(0); return true; case R.id.sort_last_played_newold: sortType = 4; mAdapter.updateData( mAdapter.generateGameList(mSearchQuery, playListType, sortType, currentYear)); mRecyclerView.scrollToPosition(0); return true; case R.id.sort_last_played_oldnew: sortType = 5; mAdapter.updateData( mAdapter.generateGameList(mSearchQuery, playListType, sortType, currentYear)); mRecyclerView.scrollToPosition(0); return true; } return false; } }); MenuInflater inflater = popup.getMenuInflater(); inflater.inflate(R.menu.games_sort, popup.getMenu()); if (playListType == 4) { //remove sort by plays popup.getMenu().getItem(2).setVisible(false); ; popup.getMenu().getItem(3).setVisible(false); ; } popup.show(); return true; case R.id.show_expansions: if (showExpansions) { //currently showing expansions //trying to hide them //make it say show expansions item.setTitle(getString(R.string.show_expansions)); item.setIcon(R.drawable.ic_visibility); playListType = playListType_Holder; mAdapter.updateData(mAdapter.generateGameList(mSearchQuery, playListType, sortType, currentYear)); if (mSearch != null) { mSearch.setHint(getString(R.string.filter) + mAdapter.getItemCount() + getString(R.string.filter_games)); } showExpansions = false; } else { //currently hiding expansions //trying to show them //make it say hide expansions item.setTitle(getString(R.string.hide_expansions)); item.setIcon(R.drawable.ic_visibility_off); playListType_Holder = playListType; if (playListType == 4) { playListType = 5; } else { playListType = 3; } mAdapter.updateData(mAdapter.generateGameList(mSearchQuery, playListType, sortType, currentYear)); if (mSearch != null) { mSearch.setHint(getString(R.string.filter) + mAdapter.getItemCount() + getString(R.string.filter_games)); } showExpansions = true; } return true; } return false; }
From source file:org.mozilla.gecko.BrowserApp.java
@Override public boolean onPrepareOptionsMenu(Menu aMenu) { if (aMenu == null) return false; // Hide the tab history panel when hardware menu button is pressed. TabHistoryFragment frag = (TabHistoryFragment) getSupportFragmentManager() .findFragmentByTag(TAB_HISTORY_FRAGMENT_TAG); if (frag != null) { frag.dismiss();/*from w w w .ja v a 2s . co m*/ } if (!GeckoThread.checkLaunchState(GeckoThread.LaunchState.GeckoRunning)) { aMenu.findItem(R.id.settings).setEnabled(false); aMenu.findItem(R.id.help).setEnabled(false); } Tab tab = Tabs.getInstance().getSelectedTab(); final MenuItem bookmark = aMenu.findItem(R.id.bookmark); final MenuItem reader = aMenu.findItem(R.id.reading_list); final MenuItem back = aMenu.findItem(R.id.back); final MenuItem forward = aMenu.findItem(R.id.forward); final MenuItem share = aMenu.findItem(R.id.share); final MenuItem quickShare = aMenu.findItem(R.id.quickshare); final MenuItem saveAsPDF = aMenu.findItem(R.id.save_as_pdf); final MenuItem charEncoding = aMenu.findItem(R.id.char_encoding); final MenuItem findInPage = aMenu.findItem(R.id.find_in_page); final MenuItem desktopMode = aMenu.findItem(R.id.desktop_mode); final MenuItem enterGuestMode = aMenu.findItem(R.id.new_guest_session); final MenuItem exitGuestMode = aMenu.findItem(R.id.exit_guest_session); // Only show the "Quit" menu item on pre-ICS, television devices, // or if the user has explicitly enabled the clear on shutdown pref. // (We check the pref last to save the pref read.) // In ICS+, it's easy to kill an app through the task switcher. final boolean visible = Versions.preICS || HardwareUtils.isTelevision() || !PrefUtils .getStringSet(GeckoSharedPrefs.forProfile(this), ClearOnShutdownPref.PREF, new HashSet<String>()) .isEmpty(); aMenu.findItem(R.id.quit).setVisible(visible); aMenu.findItem(R.id.logins).setVisible(AppConstants.NIGHTLY_BUILD); if (tab == null || tab.getURL() == null) { bookmark.setEnabled(false); reader.setEnabled(false); back.setEnabled(false); forward.setEnabled(false); share.setEnabled(false); quickShare.setEnabled(false); saveAsPDF.setEnabled(false); findInPage.setEnabled(false); // NOTE: Use MenuUtils.safeSetEnabled because some actions might // be on the BrowserToolbar context menu. if (Versions.feature11Plus) { // There is no page menu prior to v11 resources. MenuUtils.safeSetEnabled(aMenu, R.id.page, false); } MenuUtils.safeSetEnabled(aMenu, R.id.subscribe, false); MenuUtils.safeSetEnabled(aMenu, R.id.add_search_engine, false); MenuUtils.safeSetEnabled(aMenu, R.id.site_settings, false); MenuUtils.safeSetEnabled(aMenu, R.id.add_to_launcher, false); return true; } final boolean inGuestMode = GeckoProfile.get(this).inGuestMode(); final boolean isAboutReader = AboutPages.isAboutReader(tab.getURL()); bookmark.setEnabled(!isAboutReader); bookmark.setVisible(!inGuestMode); bookmark.setCheckable(true); bookmark.setChecked(tab.isBookmark()); bookmark.setIcon(resolveBookmarkIconID(tab.isBookmark())); bookmark.setTitle(resolveBookmarkTitleID(tab.isBookmark())); reader.setEnabled(isAboutReader || !AboutPages.isAboutPage(tab.getURL())); reader.setVisible(!inGuestMode); reader.setCheckable(true); final boolean isPageInReadingList = tab.isInReadingList(); reader.setChecked(isPageInReadingList); reader.setIcon(resolveReadingListIconID(isPageInReadingList)); reader.setTitle(resolveReadingListTitleID(isPageInReadingList)); back.setEnabled(tab.canDoBack()); forward.setEnabled(tab.canDoForward()); desktopMode.setChecked(tab.getDesktopMode()); desktopMode.setIcon( tab.getDesktopMode() ? R.drawable.ic_menu_desktop_mode_on : R.drawable.ic_menu_desktop_mode_off); View backButtonView = MenuItemCompat.getActionView(back); if (backButtonView != null) { backButtonView.setOnLongClickListener(new Button.OnLongClickListener() { @Override public boolean onLongClick(View view) { Tab tab = Tabs.getInstance().getSelectedTab(); if (tab != null) { closeOptionsMenu(); return tabHistoryController.showTabHistory(tab, TabHistoryController.HistoryAction.BACK); } return false; } }); } View forwardButtonView = MenuItemCompat.getActionView(forward); if (forwardButtonView != null) { forwardButtonView.setOnLongClickListener(new Button.OnLongClickListener() { @Override public boolean onLongClick(View view) { Tab tab = Tabs.getInstance().getSelectedTab(); if (tab != null) { closeOptionsMenu(); return tabHistoryController.showTabHistory(tab, TabHistoryController.HistoryAction.FORWARD); } return false; } }); } String url = tab.getURL(); if (AboutPages.isAboutReader(url)) { String urlFromReader = ReaderModeUtils.getUrlFromAboutReader(url); if (urlFromReader != null) { url = urlFromReader; } } // Disable share menuitem for about:, chrome:, file:, and resource: URIs final boolean shareVisible = RestrictedProfiles.isAllowed(this, Restriction.DISALLOW_SHARE); share.setVisible(shareVisible); final boolean shareEnabled = StringUtils.isShareableUrl(url) && shareVisible; share.setEnabled(shareEnabled); MenuUtils.safeSetEnabled(aMenu, R.id.downloads, RestrictedProfiles.isAllowed(this, Restriction.DISALLOW_DOWNLOADS)); // NOTE: Use MenuUtils.safeSetEnabled because some actions might // be on the BrowserToolbar context menu. if (Versions.feature11Plus) { MenuUtils.safeSetEnabled(aMenu, R.id.page, !isAboutHome(tab)); } MenuUtils.safeSetEnabled(aMenu, R.id.subscribe, tab.hasFeeds()); MenuUtils.safeSetEnabled(aMenu, R.id.add_search_engine, tab.hasOpenSearch()); MenuUtils.safeSetEnabled(aMenu, R.id.site_settings, !isAboutHome(tab)); MenuUtils.safeSetEnabled(aMenu, R.id.add_to_launcher, !isAboutHome(tab)); // Action providers are available only ICS+. if (Versions.feature14Plus) { quickShare.setVisible(shareVisible); quickShare.setEnabled(shareEnabled); // This provider also applies to the quick share menu item. final GeckoActionProvider provider = ((GeckoMenuItem) share).getGeckoActionProvider(); if (provider != null) { Intent shareIntent = provider.getIntent(); // For efficiency, the provider's intent is only set once if (shareIntent == null) { shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.setType("text/plain"); provider.setIntent(shareIntent); } // Replace the existing intent's extras shareIntent.putExtra(Intent.EXTRA_TEXT, url); shareIntent.putExtra(Intent.EXTRA_SUBJECT, tab.getDisplayTitle()); shareIntent.putExtra(Intent.EXTRA_TITLE, tab.getDisplayTitle()); shareIntent.putExtra(ShareDialog.INTENT_EXTRA_DEVICES_ONLY, true); // Clear the existing thumbnail extras so we don't share an old thumbnail. shareIntent.removeExtra("share_screenshot_uri"); // Include the thumbnail of the page being shared. BitmapDrawable drawable = tab.getThumbnail(); if (drawable != null) { Bitmap thumbnail = drawable.getBitmap(); // Kobo uses a custom intent extra for sharing thumbnails. if (Build.MANUFACTURER.equals("Kobo") && thumbnail != null) { File cacheDir = getExternalCacheDir(); if (cacheDir != null) { File outFile = new File(cacheDir, "thumbnail.png"); try { java.io.FileOutputStream out = new java.io.FileOutputStream(outFile); thumbnail.compress(Bitmap.CompressFormat.PNG, 90, out); } catch (FileNotFoundException e) { Log.e(LOGTAG, "File not found", e); } shareIntent.putExtra("share_screenshot_uri", Uri.parse(outFile.getPath())); } } } } } final boolean privateTabVisible = RestrictedProfiles.isAllowed(this, Restriction.DISALLOW_PRIVATE_BROWSING); MenuUtils.safeSetVisible(aMenu, R.id.new_private_tab, privateTabVisible); // Disable save as PDF for about:home and xul pages. saveAsPDF.setEnabled(!(isAboutHome(tab) || tab.getContentType().equals("application/vnd.mozilla.xul+xml") || tab.getContentType().startsWith("video/"))); // Disable find in page for about:home, since it won't work on Java content. findInPage.setEnabled(!isAboutHome(tab)); charEncoding.setVisible(GeckoPreferences.getCharEncodingState()); if (mProfile.inGuestMode()) { exitGuestMode.setVisible(true); } else { enterGuestMode.setVisible(true); } if (!RestrictedProfiles.isAllowed(this, Restriction.DISALLOW_GUEST_BROWSING)) { MenuUtils.safeSetVisible(aMenu, R.id.new_guest_session, false); } if (!RestrictedProfiles.isAllowed(this, Restriction.DISALLOW_INSTALL_EXTENSION)) { MenuUtils.safeSetVisible(aMenu, R.id.addons, false); } return true; }
From source file:com.ichi2.anki2.DeckPicker.java
/** Handles item selections */ @Override//from w ww . j a v a 2 s . c o m public boolean onOptionsItemSelected(MenuItem item) { Resources res = getResources(); switch (item.getItemId()) { case MENU_HELP: showDialog(DIALOG_SELECT_HELP); return true; case MENU_SYNC: sync(); return true; case MENU_ADD_NOTE: addNote(); return true; case MENU_STATISTICS: showDialog(DIALOG_SELECT_STATISTICS_TYPE); return true; case MENU_CARDBROWSER: openCardBrowser(); return true; case MENU_CREATE_DECK: StyledDialog.Builder builder2 = new StyledDialog.Builder(DeckPicker.this); builder2.setTitle(res.getString(R.string.new_deck)); mDialogEditText = (EditText) new EditText(DeckPicker.this); // mDialogEditText.setFilters(new InputFilter[] { mDeckNameFilter }); builder2.setView(mDialogEditText, false, false); builder2.setPositiveButton(res.getString(R.string.create), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String deckName = mDialogEditText.getText().toString().replaceAll("[\'\"\\n\\r\\[\\]\\(\\)]", ""); Log.i(AnkiDroidApp.TAG, "Creating deck: " + deckName); AnkiDroidApp.getCol().getDecks().id(deckName, true); loadCounts(); } }); builder2.setNegativeButton(res.getString(R.string.cancel), null); builder2.create().show(); return true; case MENU_CREATE_DYNAMIC_DECK: StyledDialog.Builder builder3 = new StyledDialog.Builder(DeckPicker.this); builder3.setTitle(res.getString(R.string.new_deck)); mDialogEditText = (EditText) new EditText(DeckPicker.this); ArrayList<String> names = AnkiDroidApp.getCol().getDecks().allNames(); int n = 1; String cramDeckName = "Cram 1"; while (names.contains(cramDeckName)) { n++; cramDeckName = "Cram " + n; } mDialogEditText.setText(cramDeckName); // mDialogEditText.setFilters(new InputFilter[] { mDeckNameFilter }); builder3.setView(mDialogEditText, false, false); builder3.setPositiveButton(res.getString(R.string.create), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { long id = AnkiDroidApp.getCol().getDecks().newDyn(mDialogEditText.getText().toString()); openStudyOptions(id, new Bundle()); } }); builder3.setNegativeButton(res.getString(R.string.cancel), null); builder3.create().show(); return true; case MENU_ABOUT: startActivity(new Intent(DeckPicker.this, Info.class)); if (AnkiDroidApp.SDK_VERSION > 4) { ActivityTransitionAnimation.slide(DeckPicker.this, ActivityTransitionAnimation.RIGHT); } return true; case MENU_ADD_SHARED_DECK: if (AnkiDroidApp.getCol() != null) { SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(getBaseContext()); String hkey = preferences.getString("hkey", ""); if (hkey.length() == 0) { showDialog(DIALOG_USER_NOT_LOGGED_IN_ADD_SHARED_DECK); } else { addSharedDeck(); } } return true; case MENU_IMPORT: showDialog(DIALOG_IMPORT_HINT); return true; case MENU_PREFERENCES: startActivityForResult(new Intent(DeckPicker.this, Preferences.class), PREFERENCES_UPDATE); return true; case MENU_FEEDBACK: Intent i = new Intent(DeckPicker.this, Feedback.class); i.putExtra("request", REPORT_FEEDBACK); startActivityForResult(i, REPORT_FEEDBACK); if (AnkiDroidApp.SDK_VERSION > 4) { ActivityTransitionAnimation.slide(this, ActivityTransitionAnimation.RIGHT); } return true; case CHECK_DATABASE: integrityCheck(); return true; case StudyOptionsActivity.MENU_ROTATE: if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } else { this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } return true; case StudyOptionsActivity.MENU_NIGHT: SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(this); if (preferences.getBoolean("invertedColors", false)) { preferences.edit().putBoolean("invertedColors", false).commit(); item.setIcon(R.drawable.ic_menu_night); } else { preferences.edit().putBoolean("invertedColors", true).commit(); item.setIcon(R.drawable.ic_menu_night_checked); } return true; case MENU_REUPGRADE: restartUpgradeProcess(); return true; default: return super.onOptionsItemSelected(item); } }
From source file:com.hichinaschool.flashcards.anki.DeckPicker.java
/** Handles item selections */ @Override/* w ww . ja v a2 s . c om*/ public boolean onOptionsItemSelected(MenuItem item) { Resources res = getResources(); switch (item.getItemId()) { case MENU_HELP: showDialog(DIALOG_SELECT_HELP); return true; case MENU_SYNC: sync(); return true; case MENU_ADD_NOTE: addNote(); return true; case MENU_STATISTICS: showDialog(DIALOG_SELECT_STATISTICS_TYPE); return true; case MENU_CARDBROWSER: openCardBrowser(); return true; case MENU_CREATE_DECK: StyledDialog.Builder builder2 = new StyledDialog.Builder(DeckPicker.this); builder2.setTitle(res.getString(R.string.new_deck)); mDialogEditText = (EditText) new EditText(DeckPicker.this); // mDialogEditText.setFilters(new InputFilter[] { mDeckNameFilter }); builder2.setView(mDialogEditText, false, false); builder2.setPositiveButton(res.getString(R.string.create), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String deckName = mDialogEditText.getText().toString().replaceAll("[\'\"\\n\\r\\[\\]\\(\\)]", ""); // Log.i(AnkiDroidApp.TAG, "Creating deck: " + deckName); AnkiDroidApp.getCol().getDecks().id(deckName, true); loadCounts(); } }); builder2.setNegativeButton(res.getString(R.string.cancel), null); builder2.create().show(); return true; case MENU_CREATE_DYNAMIC_DECK: StyledDialog.Builder builder3 = new StyledDialog.Builder(DeckPicker.this); builder3.setTitle(res.getString(R.string.new_deck)); mDialogEditText = (EditText) new EditText(DeckPicker.this); ArrayList<String> names = AnkiDroidApp.getCol().getDecks().allNames(); int n = 1; String cramDeckName = "Cram 1"; while (names.contains(cramDeckName)) { n++; cramDeckName = "Cram " + n; } mDialogEditText.setText(cramDeckName); // mDialogEditText.setFilters(new InputFilter[] { mDeckNameFilter }); builder3.setView(mDialogEditText, false, false); builder3.setPositiveButton(res.getString(R.string.create), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { long id = AnkiDroidApp.getCol().getDecks().newDyn(mDialogEditText.getText().toString()); openStudyOptions(id, new Bundle()); } }); builder3.setNegativeButton(res.getString(R.string.cancel), null); builder3.create().show(); return true; case MENU_ABOUT: startActivity(new Intent(DeckPicker.this, Info.class)); if (AnkiDroidApp.SDK_VERSION > 4) { ActivityTransitionAnimation.slide(DeckPicker.this, ActivityTransitionAnimation.RIGHT); } return true; case MENU_ADD_SHARED_DECK: if (AnkiDroidApp.getCol() != null) { SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(getBaseContext()); String hkey = preferences.getString("hkey", ""); if (hkey.length() == 0) { showDialog(DIALOG_USER_NOT_LOGGED_IN_ADD_SHARED_DECK); } else { addSharedDeck(); } } return true; case MENU_IMPORT: showDialog(DIALOG_IMPORT_HINT); return true; case MENU_PREFERENCES: startActivityForResult(new Intent(DeckPicker.this, Preferences.class), PREFERENCES_UPDATE); return true; case MENU_FEEDBACK: Intent i = new Intent(DeckPicker.this, Feedback.class); i.putExtra("request", REPORT_FEEDBACK); startActivityForResult(i, REPORT_FEEDBACK); if (AnkiDroidApp.SDK_VERSION > 4) { ActivityTransitionAnimation.slide(this, ActivityTransitionAnimation.RIGHT); } return true; case CHECK_DATABASE: integrityCheck(); return true; case StudyOptionsActivity.MENU_ROTATE: if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } else { this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } return true; case StudyOptionsActivity.MENU_NIGHT: SharedPreferences preferences = AnkiDroidApp.getSharedPrefs(this); if (preferences.getBoolean("invertedColors", false)) { preferences.edit().putBoolean("invertedColors", false).commit(); item.setIcon(R.drawable.ic_menu_night); } else { preferences.edit().putBoolean("invertedColors", true).commit(); item.setIcon(R.drawable.ic_menu_night_checked); } return true; case MENU_REUPGRADE: restartUpgradeProcess(); return true; default: return super.onOptionsItemSelected(item); } }