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.timemachine.controller.ControllerActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);

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

    // Change the text color in the search view
    int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
    searchTextView = (TextView) searchView.findViewById(id);
    searchTextView.setHintTextColor(Color.parseColor("#80ffffff"));
    searchTextView.setTextColor(Color.parseColor("#ffffff"));

    return super.onCreateOptionsMenu(menu);
}

From source file:br.com.mybaby.contatos.ContactsListFragment.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override/*from w w w .  j  a  v a 2 s . c  om*/
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    // Inflate the menu items
    inflater.inflate(R.menu.contatos_list_menu, menu);
    // Locate the search item
    MenuItem searchItem = menu.findItem(R.id.menu_search);

    // In versions prior to Android 3.0, hides the search item to prevent additional
    // searches. In Android 3.0 and later, searching is done via a SearchView in the ActionBar.
    // Since the search doesn't create a new Activity to do the searching, the menu item
    // doesn't need to be turned off.
    if (mIsSearchResultView) {
        searchItem.setVisible(false);
    }

    // In version 3.0 and later, sets up and configures the ActionBar SearchView
    if (Util.hasHoneycomb()) {

        // Retrieves the system search manager service
        final SearchManager searchManager = (SearchManager) getActivity()
                .getSystemService(Context.SEARCH_SERVICE);

        // Retrieves the SearchView from the search menu item
        final SearchView searchView = (SearchView) searchItem.getActionView();

        // Assign searchable info to SearchView
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

        // Set listeners for SearchView
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String queryText) {
                // Nothing needs to happen when the user submits the search string
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                // Called when the action bar search text has changed.  Updates
                // the search filter, and restarts the loader to do a new query
                // using the new search string.
                String newFilter = !TextUtils.isEmpty(newText) ? newText : null;

                // Don't do anything if the filter is empty
                if (mSearchTerm == null && newFilter == null) {
                    return true;
                }

                // Don't do anything if the new filter is the same as the current filter
                if (mSearchTerm != null && mSearchTerm.equals(newFilter)) {
                    return true;
                }

                // Updates current filter to new filter
                mSearchTerm = newFilter;

                // Restarts the loader. This triggers onCreateLoader(), which builds the
                // necessary content Uri from mSearchTerm.
                mSearchQueryChanged = true;
                getLoaderManager().restartLoader(ContactsQuery.QUERY_ID, null, ContactsListFragment.this);
                return true;
            }
        });

        if (Util.hasICS()) {
            // This listener added in ICS
            searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
                @Override
                public boolean onMenuItemActionExpand(MenuItem menuItem) {
                    // Nothing to do when the action item is expanded
                    return true;
                }

                @Override
                public boolean onMenuItemActionCollapse(MenuItem menuItem) {
                    // When the user collapses the SearchView the current search string is
                    // cleared and the loader restarted.
                    if (!TextUtils.isEmpty(mSearchTerm)) {
                        onSelectionCleared();
                    }
                    mSearchTerm = null;
                    getLoaderManager().restartLoader(ContactsQuery.QUERY_ID, null, ContactsListFragment.this);
                    return true;
                }
            });
        }

        if (mSearchTerm != null) {
            // If search term is already set here then this fragment is
            // being restored from a saved state and the search menu item
            // needs to be expanded and populated again.

            // Stores the search term (as it will be wiped out by
            // onQueryTextChange() when the menu item is expanded).
            final String savedSearchTerm = mSearchTerm;

            // Expands the search menu item
            if (Util.hasICS()) {
                searchItem.expandActionView();
            }

            // Sets the SearchView to the previous search string
            searchView.setQuery(savedSearchTerm, false);
        }
    }
}

From source file:com.undatech.opaque.RemoteCanvasActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    Log.e(TAG, "onCreateOptionsMenu called");
    try {//from  w  ww.j  a  v  a  2  s  .c o m
        getMenuInflater().inflate(R.menu.connectedmenu, menu);

        // Check the proper input method item.
        Menu inputMenu = menu.findItem(R.id.menuInputMethod).getSubMenu();
        inputMenu.findItem(inputHandlerIdToXmlId(connection.getInputMethod())).setChecked(true);

        // Set the text of the Extra Keys menu item appropriately.
        if (connection.getExtraKeysToggleType() == Constants.EXTRA_KEYS_ON)
            menu.findItem(R.id.menuExtraKeys).setTitle(R.string.extra_keys_disable);
        else
            menu.findItem(R.id.menuExtraKeys).setTitle(R.string.extra_keys_enable);
    } catch (NullPointerException e) {
        Log.e(TAG, "There was an error: " + e.getMessage());
    }
    return true;
}

From source file:cgeo.geocaching.CacheListActivity.java

private static void setVisibleEnabled(final Menu menu, final int itemId, final boolean visible,
        final boolean enabled) {
    final MenuItem item = menu.findItem(itemId);
    item.setVisible(visible);//from w w  w  .java 2s. com
    item.setEnabled(enabled);
}

From source file:ac.robinson.paperchains.PaperChainsActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (mCurrentMode == MODE_CAPTURE) {
        // no menus or interaction if we haven't got a page yet
        return super.onCreateOptionsMenu(menu);
    }//from w ww .j a v a 2 s  . c  om

    getMenuInflater().inflate(R.menu.menu, menu);
    switch (mCurrentMode) {
    case MODE_LISTEN:
        menu.findItem(R.id.action_listen).setVisible(false);
        break;

    case MODE_ADD:
        menu.findItem(R.id.action_add_audio).setVisible(false);
        break;

    case MODE_IMAGE_ONLY:
        menu.findItem(R.id.action_listen).setVisible(false);
        menu.findItem(R.id.action_add_audio).setVisible(false);
        break;
    }
    return super.onCreateOptionsMenu(menu);
}

From source file:com.krayzk9s.imgurholo.ui.ImagesFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    ImgurHoloActivity activity = (ImgurHoloActivity) getActivity();
    if (activity.getApiCall().settings.getString("theme", MainActivity.HOLO_LIGHT)
            .equals(MainActivity.HOLO_LIGHT))
        inflater.inflate(R.menu.main, menu);
    else/*  w  w w  .  j  av  a2s. c  o  m*/
        inflater.inflate(R.menu.main_dark, menu);
    menu.findItem(R.id.action_upload).setVisible(false);
    menu.findItem(R.id.action_refresh).setVisible(true);
    menu.findItem(R.id.action_download).setVisible(true);
    if (albumId != null && galleryAlbumData == null) {
        menu.findItem(R.id.action_new).setVisible(true);
    }
    if (albumId != null && galleryAlbumData != null) {
        menu.findItem(R.id.action_comments).setVisible(true);
    }
    if (albumId != null) {
        menu.findItem(R.id.action_copy).setVisible(true);
        menu.findItem(R.id.action_share).setVisible(true);
    }
}

From source file:com.googlecode.CallerLookup.Main.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    assert ((mLookup.getSelectedItemPosition() == 0)
            || (mLookupEntries.get(mLookup.getSelectedItem().toString()) != null));
    boolean custom = (mLookup.getSelectedItemPosition() == 0);
    boolean saved = (!custom
            && mLookupEntries.get(mLookup.getSelectedItem().toString()).mName.startsWith(SAVED_PREFIX));
    boolean empty = (mURL.getText().toString().length() <= 0);

    MenuItem customize = menu.findItem(MENU_CUSTOMIZE);
    assert (customize != null);
    customize.setEnabled(!custom);//from   w w w. j  ava 2s. c  om

    MenuItem save = menu.findItem(MENU_SAVE);
    assert (save != null);
    save.setEnabled(custom && !empty);

    MenuItem remove = menu.findItem(MENU_REMOVE);
    assert (remove != null);
    remove.setEnabled(saved);

    MenuItem submit = menu.findItem(MENU_SUBMIT);
    assert (submit != null);
    submit.setEnabled((custom || saved) && !empty);

    return super.onPrepareOptionsMenu(menu);
}

From source file:net.mypapit.mobile.myrepeater.RepeaterListActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.display_map, menu);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
        searchView.setQueryHint("part of repeater callsign");

        SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener() {

            @Override//w w  w  . ja v a  2  s  .  c  o  m
            public boolean onQueryTextSubmit(String searchText) {

                adapter.getFilter().filter(searchText);
                Log.d("MYRepeater", "search: " + searchText);
                adapter.notifyDataSetChanged();
                return true;
            }

            @Override
            public boolean onQueryTextChange(String searchText) {
                // TODO Auto-generated method stub
                adapter.getFilter().filter(searchText);
                adapter.notifyDataSetChanged();
                return true;
            }
        };

        searchView.setOnQueryTextListener(textChangeListener);
    }

    return super.onCreateOptionsMenu(menu);
}

From source file:com.andreadec.musicplayer.MainActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    boolean navigationDrawerOpen = drawerLayout.isDrawerOpen(drawerContainer);
    if (currentPage == PAGE_BROWSER && !navigationDrawerOpen) {
        menu.findItem(R.id.menu_setAsBaseFolder).setVisible(true);
        menu.findItem(R.id.menu_gotoBaseFolder).setVisible(true);
    } else {/*from  w  w  w  .  j a  v  a 2  s .  c om*/
        menu.findItem(R.id.menu_setAsBaseFolder).setVisible(false);
        menu.findItem(R.id.menu_gotoBaseFolder).setVisible(false);
    }
    if (currentPage == PAGE_PODCASTS && !navigationDrawerOpen) {
        menu.findItem(R.id.menu_removeAllEpisodes).setVisible(true);
        menu.findItem(R.id.menu_removeDownloadedEpisodes).setVisible(true);
    } else {
        menu.findItem(R.id.menu_removeAllEpisodes).setVisible(false);
        menu.findItem(R.id.menu_removeDownloadedEpisodes).setVisible(false);
    }
    if (navigationDrawerOpen || musicService == null || musicService.getCurrentPlayingItem() == null) {
        menu.findItem(R.id.menu_songInfo).setVisible(false);
    } else {
        menu.findItem(R.id.menu_songInfo).setVisible(true);
    }
    return true;
}

From source file:org.openhab.habdroid.ui.OpenHABMainActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    menu.findItem(R.id.mainmenu_voice_recognition).setVisible(mVoiceRecognitionEnabled);
    return true;
}