1

When using search view, I have a requirement from customer that they want to retain the search content after reopen the search view. My Search view is on a list view and do a real timing filtering based on what user input into the search box. When closed the search box by either click the back button on the phone or click the soft back button on the top left on action bar, the search box closed, search view iconfied. But when reopen it next time, the search query used last time is also been cleared, which I do not want.

My question is that is there a way I can keep the search view content there. Just hiding the search box, but not clear the content?

My related code are as follow:

MenuItem search;
SearchView searchView;

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_locationlist_fragment, menu);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        search = menu.findItem(R.id.action_search_location_list);
        searchView = (SearchView) MenuItemCompat.getActionView(search);
        SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
        searchView.setIconifiedByDefault(false);
        searchView.setOnQueryTextListener(this);


        searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                //This will make sure, when user closed search view, the list will be restored.
                if(!hasFocus) {
                    Log.i(Tags.LOCATIONLIST,"Search Close");

                    search.collapseActionView();
                } else {

                   }
                }
            }
        });

        ImageView closeButton = (ImageView)searchView.findViewById(R.id.search_close_btn);
        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText searchEditText = (EditText)searchView.findViewById(R.id.search_src_text);
                searchEditText.setText("");
                if (((LocationListAdapter)locationListView.getAdapter())!=null) {
                    ((LocationListAdapter) locationListView.getAdapter()).getFilter().filter("");
                }
            }
        });

    }
}

@Override
          public boolean onOptionsItemSelected(MenuItem item) {
              switch (item.getItemId()) {
                  case R.id.action_search_location_list:
                      ((BaseActivity) getActivity()).onSearchRequested();
                      return true;
                  case R.id.action_refresh_location_list:
                      refreshLocationList();
                      return true;
                  default:
                      return false;
              }
          }

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

@Override
          public boolean onQueryTextChange(String s) {
              if (((LocationListAdapter)locationListView.getAdapter())!=null) {
                  if (TextUtils.isEmpty(s)) {
                      locationListView.clearTextFilter();
                  } else {
                      ((LocationListAdapter) locationListView.getAdapter()).getFilter().filter(s);
                      //locationListView.setFilterText(s.toString());
                  }
              }

              return true;
          }
Arthur Wang
  • 3,118
  • 4
  • 21
  • 29

5 Answers5

2

Use

SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    searchView.setIconified(false);

Any query text is cleared when iconified. So setIconified to false. And i have used android.widget.SearchView

pallavi
  • 432
  • 3
  • 14
1

Save your String in a variable (e.g. myWantedString) and override setOnClickListener that trigers everytime you open the SearchView and use setQuery. Your code should be:

searchView.setOnSearchClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                searchView.setQuery(myWantedString, false);
            }
        });

To save your string each time the SearchView closes implement setOnCloseListener and override onClose():

searchView.setOnCloseListener(new SearchView.OnCloseListener()
        {
            @Override
            public boolean onClose()
            {
                myWantedString = searchView.getQuery();
                return false;
            }
        });
Menelaos Kotsollaris
  • 5,776
  • 9
  • 54
  • 68
  • It seems not that easy. I have tried this approach but failed. The problem is where to put the save variables. If I put it in the onQueryTextChange. I notice that when the search box closed, this method also been called. And the string passed to it become a empty string. The problem now is how can I detect whether a search view is close or open? – Arthur Wang May 04 '15 at 19:17
  • implement `setOnCloseListener` and override `onClose`. See my updated answer – Menelaos Kotsollaris May 04 '15 at 19:28
  • Thank you for your kindly help. But still it not work. The onCloseListener never called and I do some research seems it is a bug exist on Android 3.0+. On another post they ask to use ActionViewExpand, but still, it not working on Android 5.0....I could saved the query by put it in the onQueryTextChange method, by detect whether the query is an empty string or not. But the problem is, in this way, I can't distinguish whether the 'Empty String' is by user deleting the query, or just by closing the search box. – Arthur Wang May 04 '15 at 20:24
  • `setOnSearchClickListener()` is an awesome suggestion! – Sufian Feb 03 '17 at 03:20
1

searchView.setQuery() works if was called with a delay after menu item expansion.

    MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
            // set query text with a delay
            searchView.post(new Runnable() {
                @Override
                public void run() {
                    searchView.setQuery(query, false);
                }
            });
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            return true;
        }
    });
SergeyA
  • 4,427
  • 1
  • 22
  • 15
0

You can create an Activity which can be called when the user searches and the search result can be stored in the Bundle during the callback method onPause or onSaveInstanceState , when the Activity is called once again restore it from the bundle.

Abdul Rahman K
  • 664
  • 5
  • 16
0
 MenuItem searchItem = menu.findItem(R.id.action_search);
 SearchView searchView = (SearchView) searchItem.getActionView();
 searchView.setIconified(false);
 searchView.setOnSearchClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         searchView.setQuery("SEARCH_WORD", false);
     }
 });

https://developer.android.com/reference/android/widget/SearchView Sets a listener to inform when the search button is pressed. This is only relevant when the text field is not visible by default. Calling setIconified(false) can also cause this listener to be informed.

JnJ11
  • 35
  • 5