Search code examples
androidonclicklistenersearchview

SearchView does not respond to clicks after query submit


My app contains a standard SearchView widget. As you can see from the code below, I am setting an OnClickListener, an OnCloseListener, and an OnQueryTextListener.

If I tap the SearchView, it initially responds as expected. If I enter text and press the search button the keyboard, the OnQueryTextListener fires correctly, and the keyboard is dismissed as per searchView.setIconified(true). However, if I now tap the SearchView again, the OnClickListener is not fired. The keyboard still appears and the field becomes editable, but my code in the OnClickListener is not executed.

If I use the "X" icon to close the search view after this, everything returns to normal. The next time I click on the SearchView, my listener is fired.

I have additional code that I'll need to execute every time the SearchView is clicked.

What could be causing the listener to not fire in this specific instance? Is there something else that I should be doing in OnQueryTextSubmit?

    searchView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            filterLayout.setVisibility(View.VISIBLE);
            searchView.setIconified(false);
            System.out.println("on click");
        }
    });


    searchView.setOnCloseListener(new SearchView.OnCloseListener() {
        @Override
        public boolean onClose() {
            filterLayout.setVisibility(View.INVISIBLE);

            return false;
        }
    });

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {

            searchView.setIconified(true);

            return false;
        }

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

Solution

  • I found a half-answer to this problem. If I clear the focus of the searchView when the query is submitted, then when the user next taps the searchView, the OnQueryTextFocusChangeListener gets a call. It doesn't exactly answer my original question, but it's an acceptable workaround for my case.