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.lgallardo.qbittorrentclient.RefreshListener.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    // Retrieve the SearchView and plug it into SearchManager
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
    SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default

    // Handle open/close SearchView (using an item menu)
    final MenuItem menuItem = menu.findItem(R.id.action_search);

    // When back is pressed or the SearchView is close, delete the query field
    // and close the SearchView using the item menu
    searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
        @Override/*from ww w. j  av  a2s .co m*/
        public void onFocusChange(View view, boolean queryTextFocused) {
            if (!queryTextFocused) {
                menuItem.collapseActionView();
                searchView.setQuery("", false);
                searchField = "";

                refreshSwipeLayout();
                refreshCurrent();
            }
        }
    });

    // This must be implemented to override defaul searchview behaviour, in order to
    // make it work with tablets
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {

            //Log.d("Debug", "onQueryTextSubmit - searchField: " + query);

            // false: don't actually send the query. We are going to do something different
            searchView.setQuery(query, false);

            // Set the variable we use in the intent to perform the search
            searchField = query;

            // Refresh using the search field
            refreshSwipeLayout();
            refreshCurrent();

            // Close the soft keyboard
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(menuItem.getActionView().getWindowToken(), 0);

            // Here true means, override default searchview query
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });

    // There is a bug setting the hint from searchable.xml, so force it
    searchView.setQueryHint(getResources().getString(R.string.search_hint));

    return true;
}

From source file:com.irccloud.android.activity.MainActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    if (menu != null && buffer != null && buffer.type != null && NetworkConnection.getInstance().ready) {
        if (buffer.archived == 0) {
            if (menu.findItem(R.id.menu_archive) != null)
                menu.findItem(R.id.menu_archive).setTitle(R.string.menu_archive);
        } else {//from  ww w. j a v a  2 s  .c o m
            if (menu.findItem(R.id.menu_archive) != null)
                menu.findItem(R.id.menu_archive).setTitle(R.string.menu_unarchive);
        }
        if (buffer.type.equals("channel")) {
            if (ChannelsDataSource.getInstance().getChannelForBuffer(buffer.bid) == null) {
                if (menu.findItem(R.id.menu_leave) != null)
                    menu.findItem(R.id.menu_leave).setTitle(R.string.menu_rejoin);

                if (menu.findItem(R.id.menu_archive) != null) {
                    menu.findItem(R.id.menu_archive).setVisible(true);
                    menu.findItem(R.id.menu_archive).setEnabled(true);
                }
                if (menu.findItem(R.id.menu_delete) != null) {
                    menu.findItem(R.id.menu_delete).setVisible(true);
                    menu.findItem(R.id.menu_delete).setEnabled(true);
                }
                if (menu.findItem(R.id.menu_userlist) != null) {
                    menu.findItem(R.id.menu_userlist).setEnabled(false);
                    menu.findItem(R.id.menu_userlist).setVisible(false);
                }
                if (menu.findItem(R.id.menu_ban_list) != null) {
                    menu.findItem(R.id.menu_ban_list).setVisible(false);
                    menu.findItem(R.id.menu_ban_list).setEnabled(false);
                }
            } else {
                if (menu.findItem(R.id.menu_leave) != null)
                    menu.findItem(R.id.menu_leave).setTitle(R.string.menu_leave);
                if (menu.findItem(R.id.menu_archive) != null) {
                    menu.findItem(R.id.menu_archive).setVisible(false);
                    menu.findItem(R.id.menu_archive).setEnabled(false);
                }
                if (menu.findItem(R.id.menu_delete) != null) {
                    menu.findItem(R.id.menu_delete).setVisible(false);
                    menu.findItem(R.id.menu_delete).setEnabled(false);
                }
                if (menu.findItem(R.id.menu_ban_list) != null) {
                    menu.findItem(R.id.menu_ban_list).setVisible(true);
                    menu.findItem(R.id.menu_ban_list).setEnabled(true);
                }
                if (menu.findItem(R.id.menu_userlist) != null
                        && findViewById(R.id.usersListFragment2) != null) {
                    boolean hide = true;
                    try {
                        if (conn != null && conn.getUserInfo() != null && conn.getUserInfo().prefs != null) {
                            JSONObject hiddenMap = conn.getUserInfo().prefs
                                    .getJSONObject("channel-hiddenMembers");
                            if (hiddenMap.has(String.valueOf(buffer.bid))
                                    && hiddenMap.getBoolean(String.valueOf(buffer.bid)))
                                hide = false;
                        }
                    } catch (JSONException e) {
                    }
                    if (hide) {
                        menu.findItem(R.id.menu_userlist).setEnabled(false);
                        menu.findItem(R.id.menu_userlist).setVisible(false);
                    } else {
                        menu.findItem(R.id.menu_userlist).setEnabled(true);
                        menu.findItem(R.id.menu_userlist).setVisible(true);
                    }
                }
            }
        } else if (buffer.type.equals("console")) {
            if (menu.findItem(R.id.menu_archive) != null) {
                menu.findItem(R.id.menu_archive).setVisible(false);
                menu.findItem(R.id.menu_archive).setEnabled(false);
            }
            if (server != null && server.status != null && (server.status.equalsIgnoreCase("waiting_to_retry")
                    || (server.status.contains("connected") && !server.status.startsWith("dis")))) {
                if (menu.findItem(R.id.menu_disconnect) != null)
                    menu.findItem(R.id.menu_disconnect).setTitle(R.string.menu_disconnect);
                if (menu.findItem(R.id.menu_delete) != null) {
                    menu.findItem(R.id.menu_delete).setVisible(false);
                    menu.findItem(R.id.menu_delete).setEnabled(false);
                }
            } else {
                if (menu.findItem(R.id.menu_disconnect) != null)
                    menu.findItem(R.id.menu_disconnect).setTitle(R.string.menu_reconnect);
                if (menu.findItem(R.id.menu_delete) != null) {
                    menu.findItem(R.id.menu_delete).setVisible(true);
                    menu.findItem(R.id.menu_delete).setEnabled(true);
                }
            }
        }
    }
    return super.onPrepareOptionsMenu(menu);
}

From source file:com.if3games.chessonline.DroidFish.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem item = menu.findItem(R.id.item_file_menu);
    item.setTitle(boardGestures ? R.string.option_file : R.string.tools_menu);
    if (isSinglePlayer)
        return true;
    else/*from w  ww  .j  a  v  a  2 s  .  co  m*/
        return false;
}

From source file:cgeo.geocaching.CacheDetailActivity.java

@Override
public void addContextMenu(final View view) {
    view.setOnLongClickListener(new OnLongClickListener() {

        @Override/*from ww  w  .j av  a  2 s . co  m*/
        public boolean onLongClick(final View v) {
            if (view.getId() == R.id.description || view.getId() == R.id.hint) {
                selectedTextView = (IndexOutOfBoundsAvoidingTextView) view;
                mSelectionModeActive = true;
                return false;
            }
            currentActionMode = startSupportActionMode(new ActionMode.Callback() {

                @Override
                public boolean onPrepareActionMode(final ActionMode actionMode, final Menu menu) {
                    return prepareClipboardActionMode(view, actionMode, menu);
                }

                private boolean prepareClipboardActionMode(final View view, final ActionMode actionMode,
                        final Menu menu) {
                    switch (view.getId()) {
                    case R.id.value: // coordinates, gc-code, name
                        clickedItemText = ((TextView) view).getText();
                        final CharSequence itemTitle = ((TextView) ((View) view.getParent())
                                .findViewById(R.id.name)).getText();
                        if (itemTitle.equals(res.getText(R.string.cache_coordinates))) {
                            clickedItemText = GeopointFormatter.reformatForClipboard(clickedItemText);
                        }
                        buildDetailsContextMenu(actionMode, menu, itemTitle, true);
                        return true;
                    case R.id.description:
                        // combine short and long description
                        final String shortDesc = cache.getShortDescription();
                        if (StringUtils.isBlank(shortDesc)) {
                            clickedItemText = cache.getDescription();
                        } else {
                            clickedItemText = shortDesc + "\n\n" + cache.getDescription();
                        }
                        buildDetailsContextMenu(actionMode, menu, res.getString(R.string.cache_description),
                                false);
                        return true;
                    case R.id.personalnote:
                        clickedItemText = cache.getPersonalNote();
                        buildDetailsContextMenu(actionMode, menu, res.getString(R.string.cache_personal_note),
                                true);
                        return true;
                    case R.id.hint:
                        clickedItemText = cache.getHint();
                        buildDetailsContextMenu(actionMode, menu, res.getString(R.string.cache_hint), false);
                        return true;
                    case R.id.log:
                        clickedItemText = ((TextView) view).getText();
                        buildDetailsContextMenu(actionMode, menu, res.getString(R.string.cache_logs), false);
                        return true;
                    case R.id.date: // event date
                        clickedItemText = Formatter.formatHiddenDate(cache);
                        buildDetailsContextMenu(actionMode, menu, res.getString(R.string.cache_event), true);
                        menu.findItem(R.id.menu_calendar).setVisible(cache.canBeAddedToCalendar());
                        return true;
                    }
                    return false;
                }

                @Override
                public void onDestroyActionMode(final ActionMode actionMode) {
                    currentActionMode = null;
                }

                @Override
                public boolean onCreateActionMode(final ActionMode actionMode, final Menu menu) {
                    actionMode.getMenuInflater().inflate(R.menu.details_context, menu);
                    prepareClipboardActionMode(view, actionMode, menu);
                    // Return true so that the action mode is shown
                    return true;
                }

                @Override
                public boolean onActionItemClicked(final ActionMode actionMode, final MenuItem menuItem) {
                    switch (menuItem.getItemId()) {
                    // detail fields
                    case R.id.menu_calendar:
                        CalendarAdder.addToCalendar(CacheDetailActivity.this, cache);
                        actionMode.finish();
                        return true;
                    // handle clipboard actions in base
                    default:
                        return onClipboardItemSelected(actionMode, menuItem, clickedItemText, cache);
                    }
                }
            });
            return true;
        }
    });
}

From source file:com.xmobileapp.rockplayer.RockPlayer.java

/*******************************************
 * /*from   w  w w .j a v a2 s.  c  om*/
 * Create Menu
 * 
 *******************************************/
public boolean onPrepareOptionsMenu(Menu menu) {
    /*
     * Initialize clean menu
     */
    super.onPrepareOptionsMenu(menu);
    menu.clear();

    /*
     *  Add menu items
     */
    if (this.SHUFFLE)
        menu.add(0, 0, 0, "Suffle Off");
    else
        menu.add(0, 0, 0, "Shuffle On");
    menu.add(0, 1, 1, "Search");
    menu.add(0, 5, 5, "Share");
    menu.add(0, 3, 3, "Get Art");
    menu.add(0, 4, 4, "Concerts");
    menu.add(0, 6, 6, "Playlists");
    menu.add(0, 7, 7, "Preferences");
    menu.add(0, 8, 8, "Help");
    menu.add(0, 2, 2, "Exit");

    /*
     * Add menu icons
     */
    (menu.findItem(0)).setIcon(R.drawable.ic_menu_shuffle);
    (menu.findItem(3)).setIcon(R.drawable.ic_menu_music_library);
    (menu.findItem(5)).setIcon(android.R.drawable.ic_menu_share);
    (menu.findItem(1)).setIcon(android.R.drawable.ic_menu_search);
    //(menu.findItem(3)).setIcon(android.R.drawable.ic_menu_slideshow);
    (menu.findItem(4)).setIcon(android.R.drawable.ic_menu_today);
    (menu.findItem(6)).setIcon(R.drawable.ic_mp_current_playlist_btn);
    (menu.findItem(7)).setIcon(android.R.drawable.ic_menu_preferences);
    (menu.findItem(8)).setIcon(android.R.drawable.ic_menu_help);
    (menu.findItem(2)).setIcon(android.R.drawable.ic_menu_close_clear_cancel);
    //(menu.findItem(6)).setIcon(android.R.drawable.ic_menu_preferences);
    return true;
}