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:com.github.akinaru.hcidebugger.activity.HciDebuggerActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    this.getMenuInflater().inflate(R.menu.toolbar_menu, menu);

    //clear button
    MenuItem item = menu.findItem(R.id.clear_btn);
    if (item != null) {
        item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

            @Override//from   w  w  w  .j  av  a2s.c o  m
            public boolean onMenuItemClick(MenuItem item) {
                clearAdapter();
                return true;
            }
        });
    }

    //bluetoooth scan button
    item = menu.findItem(R.id.scan_btn);
    if (item != null) {
        item.setIcon(R.drawable.ic_action_scanning);
        item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                toggleScan(item);
                return true;
            }
        });
    }

    //bluetooth state button
    item = menu.findItem(R.id.state_bt_btn);
    if (item != null) {
        item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                toggleBtState();
                return true;
            }
        });
        if (mBluetoothAdapter.isEnabled()) {
            item.setIcon(R.drawable.ic_bluetooth);
        } else {
            item.setIcon(R.drawable.ic_bluetooth_disabled);
        }
    }

    //reset snoop file button
    if (Build.VERSION.SDK_INT <= 22) {
        item = menu.findItem(R.id.reset_snoop_file);
        item.setVisible(true);
        if (item != null) {
            item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    resetSnoopFile();
                    refresh();
                    return true;
                }
            });
        }
    }

    //filter button
    item = menu.findItem(R.id.filter_btn);
    if (item != null) {
        item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                filter();
                return true;
            }
        });
    }

    //refresh button
    item = menu.findItem(R.id.refresh);
    if (item != null) {
        if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_PORTRAIT) {

            item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    refresh();
                    return true;
                }
            });
        } else {
            item.setVisible(false);
        }
    }

    item = menu.findItem(R.id.share);

    if (item != null) {
        // Fetch and store ShareActionProvider
        mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
        setSharedIntent();
    }

    return super.onCreateOptionsMenu(menu);
}

From source file:cgeo.geocaching.cgeocaches.java

private void setMenuItemLabel(final Menu menu, final int menuId, final int resIdSelection, final int resId) {
    final MenuItem menuItem = menu.findItem(menuId);
    if (menuItem == null) {
        return;/*from w  ww.j a va 2 s  . com*/
    }
    boolean hasSelection = adapter != null && adapter.getCheckedCount() > 0;
    if (hasSelection) {
        menuItem.setTitle(res.getString(resIdSelection) + " (" + adapter.getCheckedCount() + ")");
    } else {
        menuItem.setTitle(res.getString(resId));
    }
}

From source file:com.SpeechEd.SpeechEdEditActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    menu.findItem(CMD_SAVE).setVisible(true);
    menu.findItem(CMD_RESET).setVisible(true);
    menu.findItem(CMD_ABOUT).setVisible(true);
    return true;/*from   www  .ja  v a  2s  .c  o  m*/
}

From source file:androidVNC.VncCanvasActivity.java

/**
 * {@inheritDoc}//from  w  w  w. ja  va  2s . c o m
 */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(fi.aalto.openoranges.project1.mcc.R.menu.vnccanvasactivitymenu, menu);

    if (vncCanvas.scaling != null)
        menu.findItem(vncCanvas.scaling.getId()).setChecked(true);

    Menu inputMenu = menu.findItem(fi.aalto.openoranges.project1.mcc.R.id.itemInputMode).getSubMenu();

    inputModeMenuItems = new MenuItem[inputModeIds.length];
    for (int i = 0; i < inputModeIds.length; i++) {
        inputModeMenuItems[i] = inputMenu.findItem(inputModeIds[i]);
    }
    updateInputMenu();
    menu.findItem(fi.aalto.openoranges.project1.mcc.R.id.itemFollowMouse)
            .setChecked(connection.getFollowMouse());
    menu.findItem(fi.aalto.openoranges.project1.mcc.R.id.itemFollowPan).setChecked(connection.getFollowPan());
    return true;
}

From source file:gc.david.dfm.ui.MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    Mint.leaveBreadcrumb("MainActivity::onCreateOptionsMenu");
    // Inflate the options menu from XML
    final MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);

    // Expandir el EditText de la bsqueda a lo largo del ActionBar
    searchMenuItem = menu.findItem(R.id.action_search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
    // Configure the search info and add any event listeners
    final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    // Indicamos que la activity actual sea la buscadora
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setSubmitButtonEnabled(false);
    searchView.setQueryRefinementEnabled(true);
    searchView.setIconifiedByDefault(true);

    // Muestra el item de men de cargar si hay elementos en la BD
    final MenuItem loadItem = menu.findItem(R.id.action_load);
    // TODO hacerlo en segundo plano
    final List<Distance> allDistances = getApplicationDaoSession().loadAll(Distance.class);
    if (allDistances.size() == 0) {
        loadItem.setVisible(false);/*  w  ww. java  2  s  . c o  m*/
    }
    return super.onCreateOptionsMenu(menu);
}

From source file:de.geeksfactory.opacclient.frontend.AccountFragment.java

@SuppressLint("NewApi")
@Override//from   w  w w .java  2 s .  c o  m
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.activity_account, menu);
    if (app.getAccount() != null
            && (app.getApi().getSupportFlags() & OpacApi.SUPPORT_FLAG_ACCOUNT_PROLONG_ALL) != 0) {
        menu.findItem(R.id.action_prolong_all).setVisible(true);
    } else {
        menu.findItem(R.id.action_prolong_all).setVisible(false);
    }
    menu.findItem(R.id.action_refresh).setVisible(supported);
    super.onCreateOptionsMenu(menu, inflater);
}

From source file:com.dwdesign.tweetings.fragment.UserProfileFragment.java

@Override
public void onClick(final View view) {
    switch (view.getId()) {
    case R.id.follow: {
        if (mUser != null && mAccountId != mUser.getId()) {
            mFollowProgress.setVisibility(View.VISIBLE);
            mFollowButton.setVisibility(View.GONE);
            if (mFriendship.isSourceFollowingTarget()) {
                mService.destroyFriendship(mAccountId, mUser.getId());
            } else {
                mService.createFriendship(mAccountId, mUser.getId());
            }//w  ww. j  a v a 2  s .  co  m
        }
        break;
    }
    case R.id.retry: {
        reloadUserInfo();
        break;
    }
    case R.id.name_container: {
        if (mUser != null) {
        }
        break;
    }
    case R.id.profile_image_container: {
        final String profile_image_url_string = getOriginalTwitterProfileImage(
                parseString(mUser.getProfileImageURL()));
        if (profile_image_url_string == null)
            return;
        final Uri uri = Uri.parse(profile_image_url_string);
        openImage(getActivity(), uri, false);
        break;
    }
    case R.id.tweets_container: {
        if (mUser == null)
            return;
        openUserTimeline(getActivity(), mAccountId, mUser.getId(), mUser.getScreenName());
        break;
    }
    case R.id.followers_container: {
        if (mUser == null)
            return;
        openUserFollowers(getActivity(), mAccountId, mUser.getId(), mUser.getScreenName());
        break;
    }
    case R.id.friends_container: {
        if (mUser == null)
            return;
        openUserFriends(getActivity(), mAccountId, mUser.getId(), mUser.getScreenName());
        break;
    }
    case R.id.more_options: {
        if (mUser == null || mFriendship == null)
            return;
        if (!isMyActivatedAccount(getActivity(), mUser.getId())) {
            mPopupMenu = PopupMenu.getInstance(getActivity(), view);
            mPopupMenu.inflate(R.menu.action_user_profile);
            final Menu menu = mPopupMenu.getMenu();
            final MenuItem blockItem = menu.findItem(MENU_BLOCK);
            final MenuItem trackItem = menu.findItem(MENU_TRACKING);
            if (blockItem != null) {
                final Drawable blockIcon = blockItem.getIcon();
                if (mFriendship.isSourceBlockingTarget()) {
                    blockItem.setTitle(R.string.unblock);
                    blockIcon.mutate().setColorFilter(getResources().getColor(R.color.holo_blue_bright),
                            PorterDuff.Mode.MULTIPLY);
                } else {
                    blockItem.setTitle(R.string.block);
                    blockIcon.clearColorFilter();
                }
            }
            if (trackItem != null) {
                if (mPreferences.getBoolean(com.dwdesign.tweetings.Constants.PREFERENCE_KEY_PUSH_NOTIFICATIONS,
                        false) == false) {
                    trackItem.setVisible(false);
                } else {
                    if (tracking == true) {
                        trackItem.setTitle(R.string.untrack_user);
                    } else {
                        trackItem.setTitle(R.string.track_user);
                    }
                }
            }
            final MenuItem sendDirectMessageItem = menu.findItem(MENU_SEND_DIRECT_MESSAGE);
            if (sendDirectMessageItem != null) {
                sendDirectMessageItem.setVisible(mFriendship.isTargetFollowingSource());
            }
            final MenuItem wantRetweetsItem = menu.findItem(MENU_WANT_RETWEETS);
            if (mFriendship.wantRetweets() == true) {
                wantRetweetsItem.setTitle(R.string.disable_retweet);
            } else {
                wantRetweetsItem.setTitle(R.string.enable_retweet);
            }
            mPopupMenu.setOnMenuItemClickListener(this);
            mPopupMenu.show();
        }
        break;
    }
    }

}

From source file:cgeo.geocaching.cgeocaches.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);

    try {//from  w w w.  ja v a2s.  co m
        if (adapter.isSelectMode()) {
            menu.findItem(MENU_SWITCH_SELECT_MODE).setTitle(res.getString(R.string.caches_select_mode_exit))
                    .setIcon(R.drawable.ic_menu_clear_playlist);
            menu.findItem(MENU_INVERT_SELECTION).setVisible(true);
        } else {
            menu.findItem(MENU_SWITCH_SELECT_MODE).setTitle(res.getString(R.string.caches_select_mode))
                    .setIcon(R.drawable.ic_menu_agenda);
            menu.findItem(MENU_INVERT_SELECTION).setVisible(false);
        }

        final boolean isEmpty = cacheList.isEmpty();
        final boolean isConcrete = isConcreteList();

        setVisible(menu, MENU_SWITCH_SELECT_MODE, !isEmpty);
        setVisible(menu, SUBMENU_MANAGE_HISTORY, !isEmpty);
        setVisible(menu, SUBMENU_SHOW_MAP, !isEmpty);
        setVisible(menu, MENU_SORT, !isEmpty);
        setVisible(menu, MENU_REFRESH_STORED, !isEmpty && (isConcrete || type != CacheListType.OFFLINE));
        setVisible(menu, MENU_DROP_CACHES, !isEmpty);
        setVisible(menu, MENU_DROP_CACHES_AND_LIST, isConcrete && !isEmpty);
        setVisible(menu, MENU_DELETE_EVENTS, isConcrete && !isEmpty && containsEvents());
        setVisible(menu, MENU_MOVE_TO_LIST, !isEmpty);
        setVisible(menu, MENU_EXPORT, !isEmpty);
        setVisible(menu, MENU_REMOVE_FROM_HISTORY, !isEmpty);
        setVisible(menu, MENU_CLEAR_OFFLINE_LOGS, !isEmpty && containsOfflineLogs());

        if (navigationMenu != null) {
            navigationMenu.setVisible(!isEmpty);
        }

        final boolean hasSelection = adapter != null && adapter.getCheckedCount() > 0;
        final boolean isNonDefaultList = isConcrete && listId != StoredList.STANDARD_LIST_ID;

        if (type == CacheListType.OFFLINE || type == CacheListType.HISTORY) { // only offline list
            setMenuItemLabel(menu, MENU_DROP_CACHES, R.string.caches_drop_selected, R.string.caches_drop_all);
            menu.findItem(MENU_DROP_CACHES_AND_LIST)
                    .setVisible(!hasSelection && isNonDefaultList && !adapter.isFiltered());
            setMenuItemLabel(menu, MENU_REFRESH_STORED, R.string.caches_refresh_selected,
                    R.string.caches_refresh_all);
            setMenuItemLabel(menu, MENU_MOVE_TO_LIST, R.string.caches_move_selected, R.string.caches_move_all);
        } else { // search and global list (all other than offline and history)
            setMenuItemLabel(menu, MENU_REFRESH_STORED, R.string.caches_store_selected,
                    R.string.caches_store_offline);
        }

        MenuItem item = menu.findItem(MENU_DROP_LIST);
        if (item != null) {
            item.setVisible(isNonDefaultList);
        }
        item = menu.findItem(MENU_RENAME_LIST);
        if (item != null) {
            item.setVisible(isNonDefaultList);
        }

        final boolean multipleLists = cgData.getLists().size() >= 2;
        item = menu.findItem(MENU_SWITCH_LIST);
        if (item != null) {
            item.setVisible(multipleLists);
        }
        item = menu.findItem(MENU_MOVE_TO_LIST);
        if (item != null) {
            item.setVisible(!isEmpty);
        }

        setMenuItemLabel(menu, MENU_REMOVE_FROM_HISTORY, R.string.cache_remove_from_history,
                R.string.cache_clear_history);
        setMenuItemLabel(menu, MENU_EXPORT, R.string.export, R.string.export);
    } catch (Exception e) {
        Log.e("cgeocaches.onPrepareOptionsMenu", e);
    }

    return true;
}

From source file:com.hichinaschool.flashcards.anki.CardEditor.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // View focus = this.getWindow().getCurrentFocus();
    // menu.findItem(MENU_LOOKUP).setEnabled(
    // focus instanceof FieldEditText
    // && ((TextView) focus).getText().length() > 0
    // && Lookup.isAvailable());

    if (mEditFields == null) {
        return false;
    }//w w  w  .  j a  v  a  2s.c o m
    for (int i = 0; i < mEditFields.size(); i++) {
        if (mEditFields.get(i).getText().length() > 0) {
            menu.findItem(MENU_COPY_CARD).setEnabled(true);
            break;
        } else if (i == mEditFields.size() - 1) {
            menu.findItem(MENU_COPY_CARD).setEnabled(false);
        }
    }

    if (mCaller != CALLER_CARDEDITOR_INTENT_ADD) {
        mIntentInformation = MetaDB.getIntentInformation(this);
        menu.findItem(MENU_SAVED_INTENT).setEnabled(mIntentInformation.size() > 0);
    }
    return true;
}