Code example for SearchView

Methods: isIconifiedonActionViewCollapsedonActionViewExpandedsetIconifiedsetQuery, requestFocus

0
                return; 
            } 
            if (mSearchMode) {
                setFocusOnSearchView(); 
            } else { 
                mSearchView.setQuery(null, false);
            } 
        } else if (flag) {
            // Everything is already set up. Still make sure the keyboard is up 
            if (mSearchView != null) setFocusOnSearchView();
        } 
    } 
 
    public String getQueryString() {
        return mSearchMode ? mQueryString : null;
    } 
 
    public void setQueryString(String query) {
        mQueryString = query;
        if (mSearchView != null) {
            mSearchView.setQuery(query, false);
        } 
    } 
 
    /** @return true if the "UP" icon is showing. */ 
    public boolean isUpShowing() { 
        return mSearchMode; // Only shown on the search mode.
    } 
 
    private void updateDisplayOptions() { 
        // All the flags we may change in this method. 
        final int MASK = ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME
                | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_CUSTOM;
 
        // The current flags set to the action bar.  (only the ones that we may change here) 
        final int current = mActionBar.getDisplayOptions() & MASK;
 
        // Build the new flags... 
        int newFlags = 0;
        newFlags |= ActionBar.DISPLAY_SHOW_TITLE;
        if (mShowHomeIcon) {
            newFlags |= ActionBar.DISPLAY_SHOW_HOME;
        } 
        if (mSearchMode) {
            newFlags |= ActionBar.DISPLAY_SHOW_HOME;
            newFlags |= ActionBar.DISPLAY_HOME_AS_UP;
            newFlags |= ActionBar.DISPLAY_SHOW_CUSTOM;
        } 
        mActionBar.setHomeButtonEnabled(mSearchMode);
 
        if (current != newFlags) {
            // Pass the mask here to preserve other flags that we're not interested here. 
            mActionBar.setDisplayOptions(newFlags, MASK);
        } 
    } 
 
    private void update() { 
        boolean isIconifiedChanging = mSearchView.isIconified() == mSearchMode;
        if (mSearchMode) {
            setFocusOnSearchView(); 
            // Since we have the {@link SearchView} in a custom action bar, we must manually handle 
            // expanding the {@link SearchView} when a search is initiated. Note that a side effect 
            // of this method is that the {@link SearchView} query text is set to empty string. 
            if (isIconifiedChanging) {
                mSearchView.onActionViewExpanded();
            } 
            if (mActionBar.getNavigationMode() != ActionBar.NAVIGATION_MODE_STANDARD) {
                mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
            } 
            if (mListener != null) {
                mListener.onAction(Action.START_SEARCH_MODE);
            } 
        } else { 
            final int currentNavigationMode = mActionBar.getNavigationMode();
            if (mActionBarNavigationMode == ActionBar.NAVIGATION_MODE_TABS
                    && currentNavigationMode != ActionBar.NAVIGATION_MODE_TABS) {
                // setNavigationMode will trigger onTabSelected() with the tab which was previously 
                // selected. 
                // The issue is that when we're first switching to the tab navigation mode after 
                // screen orientation changes, onTabSelected() will get called with the first tab 
                // (i.e. favorite), which would results in mCurrentTab getting set to FAVORITES and 
                // we'd lose restored tab. 
                // So let's just disable the callback here temporarily.  We'll notify the listener 
                // after this anyway. 
                mTabListener.mIgnoreTabSelected = true;
                mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
                mActionBar.setSelectedNavigationItem(mCurrentTab);
                mTabListener.mIgnoreTabSelected = false;
            } else if (mActionBarNavigationMode == ActionBar.NAVIGATION_MODE_LIST
                    && currentNavigationMode != ActionBar.NAVIGATION_MODE_LIST) {
                mNavigationListener.mIgnoreNavigationItemSelected = true;
                mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
                mActionBar.setSelectedNavigationItem(
                        getNavigationItemPositionFromTabPosition(mCurrentTab));
                mNavigationListener.mIgnoreNavigationItemSelected = false;
            } 
            mActionBar.setTitle(null);
            // Since we have the {@link SearchView} in a custom action bar, we must manually handle 
            // collapsing the {@link SearchView} when search mode is exited. 
            if (isIconifiedChanging) {
                mSearchView.onActionViewCollapsed();
            } 
            if (mListener != null) {
                mListener.onAction(Action.STOP_SEARCH_MODE);
                mListener.onSelectedTabChanged();
            } 
        } 
        updateDisplayOptions(); 
    } 
 
    @Override 
    public boolean onQueryTextChange(String queryString) {
        // TODO: Clean up SearchView code because it keeps setting the SearchView query, 
        // invoking onQueryChanged, setting up the fragment again, invalidating the options menu, 
        // storing the SearchView again, and etc... unless we add in the early return statements. 
        if (queryString.equals(mQueryString)) {
            return false; 
        } 
        mQueryString = queryString;
        if (!mSearchMode) {
            if (!TextUtils.isEmpty(queryString)) {
                setSearchMode(true); 
            } 
        } else if (mListener != null) {
            mListener.onAction(Action.CHANGE_SEARCH_QUERY);
        } 
 
        return true; 
    } 
 
    @Override 
    public boolean onQueryTextSubmit(String query) {
        // When the search is "committed" by the user, then hide the keyboard so the user can 
        // more easily browse the list of results. 
        if (mSearchView != null) {
            InputMethodManager imm = (InputMethodManager) mContext.getSystemService(
                    Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromWindow(mSearchView.getWindowToken(), 0);
            } 
            mSearchView.clearFocus();
        } 
        return true; 
    } 
 
    @Override 
    public boolean onClose() { 
        setSearchMode(false); 
        return false; 
    } 
 
    public void onSaveInstanceState(Bundle outState) {
        outState.putBoolean(EXTRA_KEY_SEARCH_MODE, mSearchMode);
        outState.putString(EXTRA_KEY_QUERY, mQueryString);
        outState.putInt(EXTRA_KEY_SELECTED_TAB, mCurrentTab);
    } 
 
    /** 
     * Clears the focus from the {@link SearchView} if we are in search mode. 
     * This will suppress the IME if it is visible. 
     */ 
    public void clearFocusOnSearchView() { 
        if (isSearchMode()) { 
            if (mSearchView != null) {
                mSearchView.clearFocus();
            } 
        } 
    } 
 
    public void setFocusOnSearchView() { 
        mSearchView.requestFocus();
        mSearchView.setIconified(false); // Workaround for the "IME not popping up" issue.
    } 
 
    private void saveLastTabPreference(int tab) {
        mPrefs.edit().putInt(PERSISTENT_LAST_TAB, tab).apply();
    }