Search code examples
javaandroidsearchview

How to detect whether user is just pressing backspace (delete) key in SearchView.setOnQueryTextListener?


I have a custom autocomplete function that fetches results from a remote server in the onQueryTextChange method based on the input. It is similar to JQuery autocomplete plugin that fetches results via Ajax on keyup listener.

However it is quite a waste to fetch results when the user is just pressing backspace to delete the input text he has previously entered because the returned results are usually the same in my situation. How can I prevent that from happening?

I have tried using a global boolean variable backKeyPressed to detect whether the user is pressing backspace and check it in onQueryTextChange but setOnKeyListener doesn't even work at all. No logs for "onKey" have even been shown in the logcat.

Would anyone have any ideas on how to detect if backspace key is pressed in onQueryTextChange?

private boolean backKeyPressed = false;
private Runnable autoCompleteRunnable;
private Handler = new Handler(Looper.getMainLooper());
private String enteredSuggestion = "";

public boolean onCreateOptionsMenu(Menu menu) {

    /****************/

    searchView.setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {

                    if (keyCode == KeyEvent.KEYCODE_DEL) {
                        backKeyPressed = true;
                    }else{
                        backKeyPressed = false;
                    }
                    Log.d("onKey",Boolean.toString(backKeyPressed));
                    return false;
                }
            });

            searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    /**Some functions**/
                    return false;
                }

                @Override
                public boolean onQueryTextChange(final String inputText) {
                    handler.removeCallbacks(autoCompleteRunnable);

                    if(enteredSuggestion.trim().equals(inputText.trim())){
                       Log.d("response_on_duplicated","true");
                       return false;
                    }

                    if(backKeyPressed == true){
                        Log.d("response_onKey","No");
                        return false;
                    }
                    autoCompleteRunnable = new Runnable() {
                        public void run() {
                            fetchSuggestionFromRemoteServer(inputText);
                        }
                    };
                    handler.postDelayed(autoCompleteRunnable, 3000);

                    enteredSuggestion = inputText;

                    return false;
                }
            });
}

Solution

  • If you can't get the backspace key. You could use a simple work around.

    You could store the length of your query string when a server call is made. Then next time in onQueryTextChange you can check whether the length of inputText is less than the length of server call string. If it is less then update the server call length with this new length, because if you don't next time if a user removes 2 characters and adds a 1 new character, your code will not make a server call. If you want a pseudo-code, do let me know.