Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SearchView.OnQueryTextListener OnQueryTextSubmit not fired on empty query string

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?

like image 752
tborja Avatar asked Nov 27 '12 01:11

tborja


2 Answers

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.

like image 188
Sam Avatar answered Nov 11 '22 12:11

Sam


I've an easier work around: use onQueryTextChange, but only render if it's empty.

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                    renderList(true);
                    return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                    if (searchView.getQuery().length() == 0) {
                            renderList(true);
                    }
                    return false;
            }
    });
like image 14
James Tan Avatar answered Nov 11 '22 10:11

James Tan