Android SearchView.OnQueryTextListener OnQueryTextSubmit not fired on empty query string

tborja picture tborja · Nov 27, 2012 · Viewed 51.5k times · Source

I am using Android 4.1.2. I have a SearchView widget on an ActionBar. Documentation on SearchView.OnQueryTextListener from the android developer site states that onQueryTextSubmit is fired/Called when the user submits the query. This could be due to a keypress on the keyboard or due to pressing a submit button."

This does not happen if the search query is empty. I need this to fire on an empty query to clear the search filter of a ListView. Is this a bug or am I doing something wrong?

Answer

Sam picture Sam · Nov 27, 2012

It is not a bug, the source code deliberately checks against null and empty values:

private void onSubmitQuery() {
    CharSequence query = mQueryTextView.getText();
    if (query != null && TextUtils.getTrimmedLength(query) > 0) {

However you should be able to use the OnQueryTextChange callback to clear your ListView's filterables when the user clears the search EditText.