Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restore state of android's search view widget

Im having a search view in my action bar and i want to restore the query after orientation changed. How can I do this? Setting the query from the bundle using SearchView.setQuery(query, false); doesn't work as the searchview sets its query to "" as soon as it expands.

Heres the relevant code in my onActivityCreated:

if(savedInstanceState != null && savedInstanceState.containsKey(BUNDLE_KEY_SEARCH))
    mCurFilter = savedInstanceState.getString(BUNDLE_KEY_SEARCH);

And here my onSaveInstanceState:

savedInstanceState.putString(BUNDLE_KEY_SEARCH, mCurFilter);
super.onSaveInstanceState(savedInstanceState);

So how can I restore its state as im not able to do it using setQuery? Thanks to all

like image 313
tobs Avatar asked Mar 22 '14 19:03

tobs


People also ask

What is a search view in Android Studio?

android.widget.SearchView. A widget that provides a user interface for the user to enter a search query and submit a request to a search provider. Shows a list of query suggestions or results, if available, and allows the user to pick a suggestion or result to launch into.

How do I customize SearchView?

just do your own search view, it is very simple. you then can include this layout in your activity layout file. This is a simple layout which includes a "search icon" followed by EditText, followed by "clear icon". The clear icon is shown after user types some text.


2 Answers

As a workaround, replace your

mSearchView.setQuery(mSearchText, false); 

with:

final String s = mSearchText;

mSearchView.post(new Runnable() {

    @Override
    public void run() {
        mSearchView.setQuery(s, false);
    }
});

It will set the saved string after the system has set the empty string.

like image 146
Frank Avatar answered Nov 15 '22 11:11

Frank


Yes, it is possible to restore the state of search view widget:

Use below code to save the search query text:

private String mSearchQuery;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();

    menuInflater.inflate(R.menu.search_menu, menu);

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

  searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));

    if(mSearchQuery != null){
        searchView.setIconified(true);
        searchView.onActionViewExpanded();
        searchView.setQuery(mSearchQuery, false);
        searchView.setFocusable(true);
    }

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){

        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

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

    return true;
}

 @Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putString("searchQuery", mSearchQuery);
    super.onSaveInstanceState(outState);
}

Then in OnCreate() method of activity, add following lines:

    if(savedInstanceState != null){
        mSearchQuery = savedInstanceState.getString("searchQuery");
    }
like image 27
Sagar Rathod Avatar answered Nov 15 '22 10:11

Sagar Rathod