Example usage for android.view MenuItem collapseActionView

List of usage examples for android.view MenuItem collapseActionView

Introduction

In this page you can find the example usage for android.view MenuItem collapseActionView.

Prototype

public boolean collapseActionView();

Source Link

Document

Collapse the action view associated with this menu item.

Usage

From source file:com.github.jvanhie.discogsscrobbler.SearchActivity.java

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.discogs_search, menu);
    //configure search box
    final MenuItem search = menu.findItem(R.id.search_field);
    mSearchView = (SearchView) search.getActionView();
    mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override/*  w w  w  .  ja  v a2s. c o m*/
        public boolean onQueryTextSubmit(String s) {
            menu.findItem(R.id.search_field).collapseActionView();
            mLastSearch = s;
            //pass query to search fragment
            if (mSearchType > 0) {
                mSearchFragment.search(s, mSearchTypes[mSearchType]);
            } else {
                mSearchFragment.search(s);
            }
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            return false;
        }
    });
    mSearchView.setOnSearchClickListener(new SearchView.OnClickListener() {
        @Override
        public void onClick(View view) {
            //restore last search
            if (mLastSearch != null) {
                mSearchView.setQuery(mLastSearch, false);
            }
        }
    });
    if (mSearchType > 0) {
        mSearchView.setQueryHint("Search Discogs (" + mSearchTypes[mSearchType] + ")");
    } else {
        mSearchView.setQueryHint("Search Discogs");
    }
    //config filter spinner
    final MenuItem filter = menu.findItem(R.id.search_filter);
    Spinner s = (Spinner) filter.getActionView(); // find the spinner
    ArrayAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(getSupportActionBar().getThemedContext(),
            R.array.search_filter_items, android.R.layout.simple_spinner_dropdown_item); //  create the adapter from a StringArray
    mSpinnerAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
    s.setAdapter(mSpinnerAdapter); // set the adapter
    s.setSelection(mSearchType, false);

    s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            mSearchType = i;
            PreferenceManager.getDefaultSharedPreferences(SearchActivity.this).edit()
                    .putInt(SEARCH_TYPE, mSearchType).apply();
            if (i > 0) {
                mSearchView.setQueryHint("Search Discogs (" + mSearchTypes[i] + ")");
            } else {
                mSearchView.setQueryHint("Search Discogs");
            }
            filter.collapseActionView();
            search.expandActionView();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
            //filter.collapseActionView();
        }
    });
    //make sure only one actionview is expanded
    MenuItemCompat.setOnActionExpandListener(filter, new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem menuItem) {
            //collapse search
            search.collapseActionView();
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem menuItem) {
            return true;
        }
    });
    MenuItemCompat.setOnActionExpandListener(search, new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem menuItem) {
            //collapse filter
            filter.collapseActionView();
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem menuItem) {
            return true;
        }
    });

    //only expand when the drawer is closed and initial page load
    if (!((DrawerLayout) findViewById(R.id.search_drawer_layout)).isDrawerOpen(findViewById(R.id.search_drawer))
            && !mDetailVisible) {
        search.expandActionView();
    }

    if (mSelected > 0) {

        inflater.inflate(R.menu.release_detail_search, menu);
        if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("enable_lastfm", true)) {
            inflater.inflate(R.menu.release_detail_scrobble, menu);
        }
    }

    return true;
}

From source file:com.multivoltage.musicat.MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    final MenuItem searchMenuItem;
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);
    searchMenuItem = menu.findItem(R.id.action_search);
    searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

        @Override//from   w w w  .j a v a  2  s  . com
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            if (TextUtils.isEmpty(s)) {
                EventSearch eventSearch = new EventSearch("");
                EventBus.getDefault().post(eventSearch);
            } else {
                EventSearch eventSearch = new EventSearch(s);
                EventBus.getDefault().post(eventSearch);
            }
            return true;
        }
    });

    searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean queryTextFocused) {
            if (!queryTextFocused) {
                searchMenuItem.collapseActionView();
                searchView.setQuery("", false);
            }
        }
    });
    return true;
}

From source file:de.enlightened.peris.PerisMain.java

@Override
public final boolean onCreateOptionsMenu(final Menu menu) {
    final MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    final MenuItem searchMenuItem = menu.findItem(R.id.search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);

    if (this.serverUserid == null || !getString(R.string.subforum_id).contentEquals("0")) {
        searchView.setVisibility(View.GONE);
    } else {/*from   w ww  . j a  v  a 2  s . co  m*/
        if (ThemeSetter.getForegroundDark(this.background)) {
            searchMenuItem.setIcon(R.drawable.ic_action_search_dark);
        }
    }

    searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
        public void onFocusChange(final View view, final boolean queryTextFocused) {
            if (!queryTextFocused) {
                searchMenuItem.collapseActionView();
                searchView.setQuery("", false);
            }
        }
    });

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

        public boolean onQueryTextChange(final String newText) {
            // TODO Auto-generated method stub
            return false;
        }

        @SuppressWarnings("checkstyle:requirethis")
        public boolean onQueryTextSubmit(final String query) {
            if (getActionBar() != null) {
                getActionBar().setSubtitle(baseSubtitle);
            }
            searchMenuItem.collapseActionView();
            searchView.setQuery("", false);

            final Bundle bundle = new Bundle();
            bundle.putString("subforum_name", (String) "Search - " + query);
            bundle.putString("subforum_id", (String) "search");
            bundle.putString("query", (String) query);
            bundle.putString("background", (String) background);
            bundle.putString("icon", (String) "n/a");
            bundle.putString("inTab", (String) "N");
            loadCategory(bundle, "SEARCH_QUERY", false);

            return false;
        }
    });
    return true;
}

From source file:bf.io.openshop.ux.MainActivity.java

/**
 * Prepare toolbar search view. Invoke search suggestions and handle search queries.
 *
 * @param searchItem corresponding menu item.
 */// w ww.  j a  v a 2  s .c  o  m
private void prepareSearchView(@NonNull final MenuItem searchItem) {
    final SearchView searchView = (SearchView) searchItem.getActionView();
    searchView.setSubmitButtonEnabled(true);
    SearchManager searchManager = (SearchManager) MainActivity.this.getSystemService(Context.SEARCH_SERVICE);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(MainActivity.this.getComponentName()));
    SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
        public boolean onQueryTextChange(String newText) {
            Timber.d("Search query text changed to: %s", newText);
            showSearchSuggestions(newText, searchView);
            return false;
        }

        public boolean onQueryTextSubmit(String query) {
            // Submit search query and hide search action view.
            onSearchSubmitted(query);
            searchView.setQuery("", false);
            searchView.setIconified(true);
            searchItem.collapseActionView();
            return true;
        }
    };

    searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
        @Override
        public boolean onSuggestionSelect(int position) {
            return false;
        }

        @Override
        public boolean onSuggestionClick(int position) {
            // Submit search suggestion query and hide search action view.
            MatrixCursor c = (MatrixCursor) searchSuggestionsAdapter.getItem(position);
            onSearchSubmitted(c.getString(1));
            searchView.setQuery("", false);
            searchView.setIconified(true);
            searchItem.collapseActionView();
            return true;
        }
    });
    searchView.setOnQueryTextListener(queryTextListener);
}

From source file:com.github.jvanhie.discogsscrobbler.ReleaseListActivity.java

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("enable_discogs", true)) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.discogs_list, menu);
        //configure search box
        final MenuItem search = menu.findItem(R.id.list_search);
        SearchView searchView = (SearchView) search.getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override// ww  w  .  ja  va 2s  .c om
            public boolean onQueryTextSubmit(String s) {
                menu.findItem(R.id.list_search).collapseActionView();
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                mReleaseList.filter(s);
                return false;
            }
        });
        searchView.setQueryHint("Filter your releases");
        final MenuItem filter = menu.findItem(R.id.list_filter);
        if (!mFolders) {
            mDiscogs.getFolders(new Discogs.DiscogsDataWaiter<List<Folder>>() {
                @Override
                public void onResult(boolean success, List<Folder> data) {
                    if (success && data != null) {
                        mFolders = true;
                        Spinner s = (Spinner) filter.getActionView(); // find the spinner
                        ArrayAdapter<Folder> mSpinnerAdapter = new ArrayAdapter<Folder>(
                                getSupportActionBar().getThemedContext(),
                                android.R.layout.simple_spinner_dropdown_item, data);
                        mSpinnerAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
                        s.setAdapter(mSpinnerAdapter); // set the adapter
                        s.setSelection(0, false);
                        s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                                mDiscogs.setFolderId(((Folder) adapterView.getItemAtPosition(i)).folderid);
                                //reload list with id
                                mReleaseList.loadList();
                                filter.collapseActionView();
                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> adapterView) {
                                //filter.collapseActionView();
                            }
                        });
                    }
                }
            });
        }

        //make sure only one actionview is expanded
        MenuItemCompat.setOnActionExpandListener(filter, new MenuItemCompat.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionExpand(MenuItem menuItem) {
                //collapse search
                search.collapseActionView();
                return true;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem menuItem) {
                return true;
            }
        });
        MenuItemCompat.setOnActionExpandListener(search, new MenuItemCompat.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionExpand(MenuItem menuItem) {
                //collapse search
                filter.collapseActionView();
                return true;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem menuItem) {
                return true;
            }
        });

        //s.setSelection(mSearchType,false);

        if (mSelected > 0) {
            if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("enable_lastfm", true)) {
                inflater.inflate(R.menu.release_detail_scrobble, menu);
            }
        }
    }
    return true;
}

From source file:com.cypress.cysmart.CommonFragments.ProfileScanningFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();/*from ww w.  ja  v  a2 s  .  c  o  m*/
    inflater.inflate(R.menu.global, menu);
    MenuItem item = menu.findItem(R.id.graph);
    MenuItem log = menu.findItem(R.id.log);
    final MenuItem searchMenuItem = menu.findItem(R.id.search);
    item.setVisible(false);
    log.setVisible(true);

    SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
    final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
    searchView.setIconifiedByDefault(false);
    SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String newText) {
            // this is your adapter that will be filtered

            return true;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {
            if (searchMenuItem != null) {
                searchMenuItem.collapseActionView();
                // this is your adapter that will be filtered
                if (mLeDeviceListAdapter != null) {
                    searchEnabled = true;
                    searchDeviceList(query);
                }
            }
            return false;

        }
    };
    searchView.setOnQueryTextListener(textChangeListener);
    searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean queryTextFocused) {
            if (!queryTextFocused) {
                searchMenuItem.collapseActionView();
                searchView.setQuery("", false);
            }
        }
    });

    super.onCreateOptionsMenu(menu, inflater);
}

From source file:cn.edu.wyu.documentviewer.DocumentsActivity.java

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

    final FragmentManager fm = getFragmentManager();

    final RootInfo root = getCurrentRoot();
    final DocumentInfo cwd = getCurrentDirectory();

    final MenuItem createDir = menu.findItem(R.id.menu_create_dir);
    final MenuItem search = menu.findItem(R.id.menu_search);
    final MenuItem sort = menu.findItem(R.id.menu_sort);
    final MenuItem sortSize = menu.findItem(R.id.menu_sort_size);
    final MenuItem grid = menu.findItem(R.id.menu_grid);
    final MenuItem list = menu.findItem(R.id.menu_list);
    final MenuItem settings = menu.findItem(R.id.menu_settings);

    // Open drawer means we hide most actions
    if (isRootsDrawerOpen()) {
        createDir.setVisible(false);/*from   ww w . j  a va  2  s.  c  o  m*/
        search.setVisible(false);
        sort.setVisible(false);
        grid.setVisible(false);
        list.setVisible(false);
        mIgnoreNextCollapse = true;
        search.collapseActionView();
        return true;
    }

    sort.setVisible(cwd != null);
    grid.setVisible(mState.derivedMode != MODE_GRID);
    list.setVisible(mState.derivedMode != MODE_LIST);

    if (mState.currentSearch != null) {
        // Search uses backend ranking; no sorting
        sort.setVisible(false);

        search.expandActionView();

        mSearchView.setIconified(false);
        mSearchView.clearFocus();
        mSearchView.setQuery(mState.currentSearch, false);
    } else {
        mIgnoreNextClose = true;
        mSearchView.setIconified(true);
        mSearchView.clearFocus();

        mIgnoreNextCollapse = true;
        search.collapseActionView();
    }

    // Only sort by size when visible
    sortSize.setVisible(mState.showSize);

    final boolean searchVisible;
    if (mState.action == ACTION_CREATE) {
        createDir.setVisible(cwd != null && cwd.isCreateSupported());
        searchVisible = false;

        // No display options in recent directories
        if (cwd == null) {
            grid.setVisible(false);
            list.setVisible(false);
        }

        SaveFragment.get(fm).setSaveEnabled(cwd != null && cwd.isCreateSupported());
    } else {
        createDir.setVisible(false);

        searchVisible = root != null && ((root.flags & Root.FLAG_SUPPORTS_SEARCH) != 0);
    }

    // TODO: close any search in-progress when hiding
    search.setVisible(searchVisible);

    settings.setVisible(mState.action != ACTION_MANAGE);

    return true;
}

From source file:dev.dworks.apps.anexplorer.DocumentsActivity.java

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

    final FragmentManager fm = getFragmentManager();

    final RootInfo root = getCurrentRoot();
    final DocumentInfo cwd = getCurrentDirectory();

    final MenuItem createDir = menu.findItem(R.id.menu_create_dir);
    //final MenuItem createFile = menu.findItem(R.id.menu_create_file);
    final MenuItem search = menu.findItem(R.id.menu_search);
    final MenuItem sort = menu.findItem(R.id.menu_sort);
    final MenuItem sortSize = menu.findItem(R.id.menu_sort_size);
    final MenuItem grid = menu.findItem(R.id.menu_grid);
    final MenuItem list = menu.findItem(R.id.menu_list);
    final MenuItem settings = menu.findItem(R.id.menu_settings);

    // Open drawer means we hide most actions
    if (isRootsDrawerOpen()) {
        createDir.setVisible(false);/*from   ww  w  .ja  v a  2 s.c om*/
        //createFile.setVisible(false);
        search.setVisible(false);
        sort.setVisible(false);
        grid.setVisible(false);
        list.setVisible(false);
        mIgnoreNextCollapse = true;
        search.collapseActionView();
        return true;
    }

    sort.setVisible(cwd != null);
    grid.setVisible(mState.derivedMode != MODE_GRID);
    list.setVisible(mState.derivedMode != MODE_LIST);

    if (mState.currentSearch != null) {
        // Search uses backend ranking; no sorting
        sort.setVisible(false);

        search.expandActionView();

        mSearchView.setIconified(false);
        mSearchView.clearFocus();
        mSearchView.setQuery(mState.currentSearch, false);
    } else {
        mIgnoreNextClose = true;
        mSearchView.setIconified(true);
        mSearchView.clearFocus();

        mIgnoreNextCollapse = true;
        search.collapseActionView();
    }

    // Only sort by size when visible
    sortSize.setVisible(mState.showSize);

    final boolean searchVisible;
    if (mState.action == ACTION_CREATE) {
        createDir.setVisible(cwd != null && cwd.isCreateSupported());
        //createFile.setVisible(cwd != null && cwd.isCreateSupported());
        searchVisible = false;

        // No display options in recent directories
        if (cwd == null) {
            grid.setVisible(false);
            list.setVisible(false);
        }
        if (null != SaveFragment.get(fm))
            SaveFragment.get(fm).setSaveEnabled(cwd != null && cwd.isCreateSupported());
    } else if (mState.action == ACTION_GET_CONTENT) {
        createDir.setVisible(false);
        searchVisible = root != null && ((root.flags & Root.FLAG_SUPPORTS_SEARCH) != 0);
    } else {
        createDir.setVisible(cwd != null && cwd.isCreateSupported());
        //createFile.setVisible(cwd != null && cwd.isCreateSupported());
        //createDir.setVisible(false);

        searchVisible = root != null && ((root.flags & Root.FLAG_SUPPORTS_SEARCH) != 0);

        if (null != SaveFragment.get(fm))
            SaveFragment.get(fm).setSaveEnabled(cwd != null && cwd.isCreateSupported());

        if (null != MoveFragment.get(fm))
            MoveFragment.get(fm).setSaveEnabled(cwd != null && cwd.isEditSupported());
    }

    // TODO: close any search in-progress when hiding
    search.setVisible(searchVisible);

    settings.setVisible(mState.action != ACTION_MANAGE);

    return true;
}

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  w w  w.  j  a v  a  2s.c  o  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;
}