Home » Android » The onQueryTextSubmit in SearchView is processed twice in Android Java

The onQueryTextSubmit in SearchView is processed twice in Android Java

Posted by: admin June 15, 2020 Leave a comment

Questions:

Why the onQueryTextSubmit method in SearchView is processed twice?
I need one result, how can I do it?

This is my code:

 public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            if (query != null)
                audioRequest(query);
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });
    return true;
}
How to&Answers:

You can use the following code to prevent onQueryTextSubmit from getting executed twice:

searchView.clearFocus();

Answer:

It generates two outputs when entered from the keyboard on the emulator but it creates single output when clicked on the search button on the keypad of the emulator. So, I think you should not worry about this. On all mobile phones or tablets, this error might not going to happen.

But, searchView.clearFocus(); also works here.

Answer:

Want to search more than 1 word?

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

        @Override
        public boolean onQueryTextChange(String newText) {


            newText = newText.toLowerCase();

            final ArrayList<DictObjectModel> filteredList = new ArrayList<DictObjectModel>();

            for (int i = 0; i < wordcombimelist.size(); i++) {

                final String text = wordcombimelist.get(i).toLowerCase();
                if (text.contains(newText)) {

                    filteredList.add(new DictObjectModel(wordcombimelist.get(i),meancombimelist.get(i)));
                }
            }
            adapter = new CustomAdapter(filteredList);
            recyclerView.setAdapter(adapter);


            return true;
        }
    });

Answer:

SearchView has the following OnEditorActionListener on Android API 28:

private final OnEditorActionListener mOnEditorActionListener = new OnEditorActionListener() {

    /**
     * Called when the input method default action key is pressed.
     */
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        onSubmitQuery();
        return true;
    }
};

And in my debugger, I see that onEditorAction is called with both KeyEvent.action == KeyEvent.ACTION_DOWN and then KeyEvent.action == ACTION_UP.

This seems like a bug in SearchView.

Answer:

Just a shot. Since you’re handling the search by yourself, try returning true instead of false.

@Override
public boolean onQueryTextSubmit(String query) {
    if (query != null)
        audioRequest(query);
    return true;
}

http://developer.android.com/reference/android/widget/SearchView.OnQueryTextListener.html

Answer:

you must make empty searchview after calling audioRequest method.
change your code as below :

public boolean onQueryTextSubmit(String query) {
        if (query != null)
            audioRequest(query);
            searchView.setQuery(null,false);

        return true;
    }

hope this help you!!

Answer:

try to call your method after the text changed like this

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

        @Override
        public boolean onQueryTextChange(String newText) {
            if (newText != null)
                audioRequest(newText);
            return false;
        }
    });

Answer:

For everyone who is struggling with the same issue, here’s what the documentation says about the onQueryTextSubmit(String query) method here :

The listener can override the standard behavior by returning true to
indicate that it has handled the submit request.

So simply replace return false by return true :

@Override
public boolean onQueryTextSubmit(String query) {
    if (query != null)
        audioRequest(query);
    return true;
}