Example usage for android.view Menu findItem

List of usage examples for android.view Menu findItem

Introduction

In this page you can find the example usage for android.view Menu findItem.

Prototype

public MenuItem findItem(int id);

Source Link

Document

Return the menu item with a particular identifier.

Usage

From source file:in.shick.diode.threads.ThreadsListActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // This happens when the user begins to hold down the menu key, so
    // allow them to chord to get a shortcut.
    mCanChord = true;/*from www . j a  va  2 s  .  co m*/

    super.onPrepareOptionsMenu(menu);

    MenuItem src, dest;

    // Login/Logout
    if (mSettings.isLoggedIn()) {
        menu.findItem(R.id.login_menu_id).setVisible(false);

        if (!mSubreddit.equals(Constants.FRONTPAGE_STRING)) {
            if (!mSubreddit.equals(Constants.REDDIT_SAVED_STRING)) {
                ArrayList<SubredditInfo> mSubredditsList = CacheInfo
                        .getCachedSubredditList(getApplicationContext());
                SubredditInfo key = new SubredditInfo();
                key.name = mSubreddit;

                if (mSubredditsList != null && mSubredditsList.contains(key)) {
                    menu.findItem(R.id.unsubscribe_menu_id).setVisible(true);
                    menu.findItem(R.id.subscribe_menu_id).setVisible(false);
                } else {
                    menu.findItem(R.id.subscribe_menu_id).setVisible(true);
                    menu.findItem(R.id.unsubscribe_menu_id).setVisible(false);
                }
                menu.findItem(R.id.sort_by_menu_id).setVisible(true);
                menu.findItem(R.id.open_browser_menu_id).setVisible(true);
            } else {
                // These menu items make no sense when viewing the saved posts.
                menu.findItem(R.id.unsubscribe_menu_id).setVisible(false);
                menu.findItem(R.id.subscribe_menu_id).setVisible(false);
                menu.findItem(R.id.sort_by_menu_id).setVisible(false);
                menu.findItem(R.id.open_browser_menu_id).setVisible(false);
            }
        }

        menu.findItem(R.id.inbox_menu_id).setVisible(true);
        menu.findItem(R.id.user_profile_menu_id).setVisible(true);
        menu.findItem(R.id.user_profile_menu_id).setTitle(
                String.format(getResources().getString(R.string.user_profile), mSettings.getUsername()));
        menu.findItem(R.id.logout_menu_id).setVisible(true);
        menu.findItem(R.id.logout_menu_id)
                .setTitle(String.format(getResources().getString(R.string.logout), mSettings.getUsername()));
        menu.findItem(R.id.saved_comments_menu_id).setVisible(true);
    } else {
        menu.findItem(R.id.login_menu_id).setVisible(true);

        menu.findItem(R.id.unsubscribe_menu_id).setVisible(false);
        menu.findItem(R.id.subscribe_menu_id).setVisible(false);

        menu.findItem(R.id.inbox_menu_id).setVisible(false);
        menu.findItem(R.id.user_profile_menu_id).setVisible(false);
        menu.findItem(R.id.logout_menu_id).setVisible(false);
        menu.findItem(R.id.saved_comments_menu_id).setVisible(false);
    }

    // Theme: Light/Dark
    src = Util.isLightTheme(mSettings.getTheme()) ? menu.findItem(R.id.dark_menu_id)
            : menu.findItem(R.id.light_menu_id);
    dest = menu.findItem(R.id.light_dark_menu_id);
    dest.setTitle(src.getTitle());

    // Sort
    if (Constants.ThreadsSort.SORT_BY_HOT_URL.equals(mSortByUrl))
        src = menu.findItem(R.id.sort_by_hot_menu_id);
    else if (Constants.ThreadsSort.SORT_BY_NEW_URL.equals(mSortByUrl))
        src = menu.findItem(R.id.sort_by_new_menu_id);
    else if (Constants.ThreadsSort.SORT_BY_CONTROVERSIAL_URL.equals(mSortByUrl))
        src = menu.findItem(R.id.sort_by_controversial_menu_id);
    else if (Constants.ThreadsSort.SORT_BY_TOP_URL.equals(mSortByUrl))
        src = menu.findItem(R.id.sort_by_top_menu_id);
    dest = menu.findItem(R.id.sort_by_menu_id);
    dest.setTitle(src.getTitle());

    return true;
}

From source file:com.dwdesign.tweetings.activity.ComposeActivity.java

private void setMenu() {
    final Menu menu = mMenuBar.getMenu();
    final int activated_color = getResources().getColor(R.color.holo_blue_bright);
    final MenuItem itemAddImage = menu.findItem(MENU_ADD_IMAGE);
    final MenuItem itemLibrary = menu.findItem(MENU_LIBRARY_MENU);
    final Drawable iconAddImage = itemAddImage.getIcon().mutate();
    final Drawable iconLibrary = itemLibrary.getIcon().mutate();
    boolean menuSelected = false;
    if (mIsImageAttached && !mIsPhotoAttached) {
        iconAddImage.setColorFilter(activated_color, Mode.MULTIPLY);
        itemAddImage.setTitle(R.string.remove_image);
        menuSelected = true;//from   w w  w .  j a  v a2s . com
    } else {
        iconAddImage.clearColorFilter();
        itemAddImage.setTitle(R.string.add_image);
    }
    final MenuItem itemTakePhoto = menu.findItem(MENU_TAKE_PHOTO);
    final Drawable iconTakePhoto = itemTakePhoto.getIcon().mutate();
    if (!mIsImageAttached && mIsPhotoAttached) {
        iconTakePhoto.setColorFilter(activated_color, Mode.MULTIPLY);
        itemTakePhoto.setTitle(R.string.remove_photo);
        menuSelected = true;
    } else {
        iconTakePhoto.clearColorFilter();
        itemTakePhoto.setTitle(R.string.take_photo);
    }
    if (menuSelected == true) {
        iconLibrary.setColorFilter(activated_color, Mode.MULTIPLY);
    } else {
        iconLibrary.clearColorFilter();
    }
    final MenuItem itemAttachLocation = menu.findItem(MENU_ADD_LOCATION);
    final Drawable iconAttachLocation = itemAttachLocation.getIcon().mutate();
    final boolean attach_location = mPreferences.getBoolean(PREFERENCE_KEY_ATTACH_LOCATION, false);
    if (attach_location && getLocation()) {
        iconAttachLocation.setColorFilter(activated_color, Mode.MULTIPLY);
        itemAttachLocation.setTitle(R.string.remove_location);
    } else {
        mPreferences.edit().putBoolean(PREFERENCE_KEY_ATTACH_LOCATION, false).commit();
        iconAttachLocation.clearColorFilter();
        itemAttachLocation.setTitle(R.string.add_location);
    }
    final MenuItem itemMore = menu.findItem(R.id.more_submenu);

    boolean moreHighlighted = false;
    if (itemMore != null) {
        final MenuItem itemDrafts = menu.findItem(R.id.drafts);
        final MenuItem itemToggleSensitive = menu.findItem(MENU_TOGGLE_SENSITIVE);
        if (itemMore != null) {
            if (itemDrafts != null) {
                final Cursor drafts_cur = getContentResolver().query(Drafts.CONTENT_URI, new String[0], null,
                        null, null);
                final Drawable iconMore = itemMore.getIcon().mutate();
                final Drawable iconDrafts = itemDrafts.getIcon().mutate();
                if (drafts_cur.getCount() > 0) {
                    moreHighlighted = true;
                    iconDrafts.setColorFilter(activated_color, Mode.MULTIPLY);
                } else {
                    iconDrafts.clearColorFilter();
                }
                drafts_cur.close();
            }
            if (itemToggleSensitive != null) {
                final boolean has_media = (mIsImageAttached || mIsPhotoAttached) && mImageUri != null;
                if (has_media && isNullOrEmpty(mUploadProvider)) {
                    itemToggleSensitive.setVisible(true);
                } else {
                    itemToggleSensitive.setVisible(false);
                }
                if (has_media && isNullOrEmpty(mUploadProvider)) {
                    final Drawable iconToggleSensitive = itemToggleSensitive.getIcon().mutate();
                    if (mIsPossiblySensitive) {
                        moreHighlighted = true;
                        iconToggleSensitive.setColorFilter(activated_color, Mode.MULTIPLY);
                    } else {
                        iconToggleSensitive.clearColorFilter();
                    }
                }
            }
        }
    }
    if (mScheduleDate != null) {
        final MenuItem itemSchedule = menu.findItem(R.id.schedule_tweet);
        final Drawable iconSchedule = itemSchedule.getIcon().mutate();
        iconSchedule.setColorFilter(activated_color, Mode.MULTIPLY);
        moreHighlighted = true;
        itemSchedule.setTitle(getString(R.string.schedule_clear));
    } else {
        final MenuItem itemSchedule = menu.findItem(R.id.schedule_tweet);
        final Drawable iconSchedule = itemSchedule.getIcon().mutate();
        iconSchedule.clearColorFilter();
        itemSchedule.setTitle(getString(R.string.schedule_tweet));
    }

    final String consumer_key = mPreferences.getString(PREFERENCE_KEY_CONSUMER_KEY, null);
    final String consumer_secret = mPreferences.getString(PREFERENCE_KEY_CONSUMER_SECRET, null);

    if (!isNullOrEmpty(consumer_key) && !isNullOrEmpty(consumer_secret)) {
        final MenuItem itemSchedule = menu.findItem(R.id.schedule_tweet);

        itemSchedule.setVisible(false);
    }

    final MenuItem bufferItem = menu.findItem(MENU_ADD_TO_BUFFER);
    if (bufferItem != null) {
        final String buffer_authorised = mPreferences.getString(PREFERENCE_KEY_BUFFERAPP_ACCESS_TOKEN, null);
        final Drawable iconBuffer = bufferItem.getIcon().mutate();
        if (buffer_authorised != null && !buffer_authorised.equals("")) {
            bufferItem.setVisible(true);
            if (mIsBuffer) {
                iconBuffer.setColorFilter(activated_color, Mode.MULTIPLY);
                moreHighlighted = true;
            } else {
                iconBuffer.clearColorFilter();
            }
        } else {
            bufferItem.setVisible(false);
            mIsBuffer = false;
        }
    }
    if (itemMore != null) {
        final Drawable iconMore = itemMore.getIcon().mutate();
        if (moreHighlighted == true) {
            iconMore.setColorFilter(activated_color, Mode.MULTIPLY);
        } else {
            iconMore.clearColorFilter();
        }
    }
    mMenuBar.invalidate();
    invalidateSupportOptionsMenu();
}

From source file:com.gelakinetic.mtgfam.fragments.CardViewFragment.java

/**
 * Prepare the Screen's standard options menu to be displayed.  This is
 * called right before the menu is shown, every time it is shown.  You can
 * use this method to efficiently enable/disable items or otherwise
 * dynamically modify the contents.//from ww w.j av a 2  s .c o  m
 *
 * @param menu The options menu as last shown or first initialized by
 *             onCreateOptionsMenu().
 * @see #setHasOptionsMenu
 * @see #onCreateOptionsMenu
 */
@Override
public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);

    MenuItem mi;
    /* If the image has been loaded to the main page, remove the menu option for image */
    if (mActivity.mPreferenceAdapter.getPicFirst() && mCardBitmap != null) {
        mi = menu.findItem(R.id.image);
        if (mi != null) {
            menu.removeItem(mi.getItemId());
        }
    }
    /* This code removes the "change set" button if there is only one set.
     * Turns out some users use it to view the full set name when there is only one set/
     * I'm leaving it here, but commented, for posterity */
    /*
     if (mPrintings != null && mPrintings.size() == 1) {
    mi = menu.findItem(R.id.changeset);
    if (mi != null) {
        menu.removeItem(mi.getItemId());
    }
    }
    */
}

From source file:com.dycody.android.idealnote.ListFragment.java

/**
 * SearchView initialization. It's a little complex because it's not using SearchManager but is implementing on its
 * own./*from   ww w  . ja v  a2s .c o  m*/
 */
@SuppressLint("NewApi")
private void initSearchView(final Menu menu) {

    // Prevents some mysterious NullPointer on app fast-switching
    if (mainActivity == null)
        return;

    // Save item as class attribute to make it collapse on drawer opening
    searchMenuItem = menu.findItem(R.id.menu_search);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager = (SearchManager) mainActivity.getSystemService(Context.SEARCH_SERVICE);
    searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.menu_search));
    searchView.setSearchableInfo(searchManager.getSearchableInfo(mainActivity.getComponentName()));
    searchView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);

    // Expands the widget hiding other actionbar icons
    searchView.setOnQueryTextFocusChangeListener((v, hasFocus) -> setActionItemsVisibility(menu, hasFocus));

    MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() {

        boolean searchPerformed = false;

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            // Reinitialize notes list to all notes when search is collapsed
            searchQuery = null;
            if (searchLayout.getVisibility() == View.VISIBLE) {
                toggleSearchLabel(false);
            }
            mainActivity.getIntent().setAction(Intent.ACTION_MAIN);
            initNotesList(mainActivity.getIntent());
            mainActivity.supportInvalidateOptionsMenu();
            return true;
        }

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {

            searchView.setOnQueryTextListener(new OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String arg0) {

                    return prefs.getBoolean("settings_instant_search", false);
                }

                @Override
                public boolean onQueryTextChange(String pattern) {

                    if (prefs.getBoolean("settings_instant_search", false) && searchLayout != null
                            && searchPerformed && mFragment.isAdded()) {
                        searchTags = null;
                        searchQuery = pattern;
                        NoteLoaderTask.getInstance().execute("getNotesByPattern", pattern);
                        return true;
                    } else {
                        searchPerformed = true;
                        return false;
                    }
                }
            });
            return true;
        }
    });
}

From source file:de.sourcestream.movieDB.MainActivity.java

/**
 * Called when invalidateOptionsMenu() is triggered
 * Prepare the Screen's standard options menu to be displayed.
 * This is called right before the menu is shown, every time it is shown.
 * You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.
 *
 * @param menu The options menu as last shown or first initialized by onCreateOptionsMenu().
 * @return You must return true for the menu to be displayed; if you return false it will not be shown.
 * If the navigation drawer is opened we hide the search view.
 * If we are on genres or about view we hide the search view.
 *///from  w w  w  . j  av  a2 s .  co  m
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    if (isDrawerOpen)
        menu.findItem(R.id.search).setVisible(false);
    else if (oldPos == 4)
        menu.findItem(R.id.search).setVisible(false);
    else
        menu.findItem(R.id.search).setVisible(true);

    return super.onPrepareOptionsMenu(menu);
}

From source file:biz.bokhorst.xprivacy.ActivityApp.java

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    MenuInflater inflater = getMenuInflater();
    if (inflater != null && PrivacyService.checkClient()) {
        inflater.inflate(R.menu.app, menu);

        // Add all contact groups
        menu.findItem(R.id.menu_contacts).getSubMenu().add(-1, R.id.menu_contacts, Menu.NONE,
                getString(R.string.menu_all));

        // Add other contact groups in the background
        new AsyncTask<Object, Object, Object>() {
            @Override/*ww  w  .  j  av a 2s.c  o m*/
            protected Object doInBackground(Object... arg0) {
                try {
                    String where = ContactsContract.Groups.GROUP_VISIBLE + " = 1";
                    where += " AND " + ContactsContract.Groups.SUMMARY_COUNT + " > 0";
                    Cursor cursor = getContentResolver().query(ContactsContract.Groups.CONTENT_SUMMARY_URI,
                            new String[] { ContactsContract.Groups._ID, ContactsContract.Groups.TITLE,
                                    ContactsContract.Groups.ACCOUNT_NAME,
                                    ContactsContract.Groups.SUMMARY_COUNT },
                            where, null,
                            ContactsContract.Groups.TITLE + ", " + ContactsContract.Groups.ACCOUNT_NAME);

                    if (cursor != null)
                        try {
                            while (cursor.moveToNext()) {
                                int id = cursor.getInt(cursor.getColumnIndex(ContactsContract.Groups._ID));
                                String title = cursor
                                        .getString(cursor.getColumnIndex(ContactsContract.Groups.TITLE));
                                String account = cursor
                                        .getString(cursor.getColumnIndex(ContactsContract.Groups.ACCOUNT_NAME));
                                menu.findItem(R.id.menu_contacts).getSubMenu().add(id, R.id.menu_contacts,
                                        Menu.NONE, title + "/" + account);
                            }
                        } finally {
                            cursor.close();
                        }
                } catch (Throwable ex) {
                    Util.bug(null, ex);
                }

                return null;
            }
        }.executeOnExecutor(mExecutor);

        return true;
    } else
        return false;
}

From source file:com.Beat.RingdroidEditActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    menu.findItem(CMD_SAVE).setVisible(true);
    menu.findItem(CMD_RESET).setVisible(true);
    menu.findItem(CMD_ABOUT).setVisible(true);
    return true;//from   w  w w. j a  v a2  s. c o  m
}

From source file:de.sourcestream.movieDB.MainActivity.java

/**
 * Initialize the contents of the Activity's standard options menu.
 * This is only called once, the first time the options menu is displayed.
 *
 * @param menu the options menu in which we place our items.
 * @return You must return true for the menu to be displayed; if you return false it will not be shown.
 *///from w  w  w  . j a  v a2s  .  co m
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    searchViewItem = menu.findItem(R.id.search);
    searchView = (SearchView) MenuItemCompat.getActionView(searchViewItem);

    searchView.setQueryHint(getResources().getString(R.string.search_hint));
    searchView.setOnQueryTextListener(searchViewOnQueryTextListener);
    searchView.setOnSuggestionListener(searchSuggestionListener);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchViewItemC = (SearchView) menu.findItem(R.id.search).getActionView();
    searchViewItemC.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    String[] from = { SearchManager.SUGGEST_COLUMN_ICON_1, SearchManager.SUGGEST_COLUMN_TEXT_1,
            SearchManager.SUGGEST_COLUMN_TEXT_2 };
    int[] to = { R.id.posterPath, R.id.title, R.id.info };
    searchAdapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.suggestionrow, null, from, to,
            0) {
        @Override
        public void changeCursor(Cursor cursor) {
            super.swapCursor(cursor);
        }
    };
    searchViewItemC.setSuggestionsAdapter(searchAdapter);

    MenuItemCompat.setOnActionExpandListener(searchViewItem, onSearchViewItemExpand);

    return true;
}

From source file:com.aimfire.gallery.GalleryActivity.java

/**
 * Override Activity lifecycle method./*from  w w w. j a v  a 2  s.  co m*/
 */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem sbs = menu.findItem(R.id.action_sbs);
    MenuItem tv = menu.findItem(R.id.action_3dtv);
    MenuItem ag = menu.findItem(R.id.action_anaglyph);
    MenuItem sw = menu.findItem(R.id.action_swap);
    MenuItem gs = menu.findItem(R.id.action_grayscale);

    switch (mDisplayMode) {
    case SbsFull:
        sbs.setChecked(true);
        sw.setEnabled(true);
        if (mDisplaySwap) {
            sbs.setIcon(R.drawable.ic_crossed_eye);
            sbs.setTitle(R.string.action_sbs_cross);
        } else {
            sbs.setIcon(R.drawable.ic_parallel_eye_white);
            sbs.setTitle(R.string.action_sbs_parallel);
        }
        break;
    case SbsHalf:
        tv.setChecked(true);
        sw.setEnabled(false);
        break;
    case Anaglyph:
        ag.setChecked(true);
        sw.setEnabled(true);
        if (mDisplaySwap) {
            ag.setIcon(R.drawable.ic_cyan_red);
            ag.setTitle(R.string.action_anaglyph_cyanred);
        } else {
            ag.setIcon(R.drawable.ic_red_cyan);
            ag.setTitle(R.string.action_anaglyph_redcyan);
        }
        break;
    default:
        break;
    }

    if (mDisplaySwap) {
        sw.setChecked(true);
    } else {
        sw.setChecked(false);
    }

    if (mDisplayColor) {
        gs.setChecked(false);
    } else {
        gs.setChecked(true);
    }
    return super.onPrepareOptionsMenu(menu);
}

From source file:android.support.v7.widget.Toolbar.java

@Override
protected void onRestoreInstanceState(Parcelable state) {
    final SavedState ss = (SavedState) state;
    super.onRestoreInstanceState(ss.getSuperState());

    final Menu menu = mMenuView != null ? mMenuView.peekMenu() : null;
    if (ss.expandedMenuItemId != 0 && mExpandedMenuPresenter != null && menu != null) {
        final MenuItem item = menu.findItem(ss.expandedMenuItemId);
        if (item != null) {
            MenuItemCompat.expandActionView(item);
        }//  w  w  w .j a v a  2  s. c o  m
    }

    if (ss.isOverflowOpen) {
        postShowOverflowMenu();
    }
}