Android SearchView empty string

I am trying to use SearchView, and I have everything to work, except when I want to search for an empty string.
OnQueryTextChange reacts when I delete the last character, but I want the user to be able to click the search button when the search field is empty.

 final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextChange(String newText) {
                    // Do something
                    return true;
                }

                @Override
                public boolean onQueryTextSubmit(String query) {
                    // Do something
                    return true;
                }
            };

        searchView.setOnQueryTextListener(queryTextListener);

I also tried using OnKeyListner. but it doesn’t work either.

            searchView.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(View arg0, int arg1, KeyEvent arg2) {

                //Do something
                return true;
            }
        }); 

It seems like such a simple thing, but I can't get it to work. Any suggestions?

Edit

I searched for a solution for a while and a few minutes after posting this question I found a solution.
In this thread, I found out that this is not a mistake, but in fact it was intentional.

Android SearchView.OnQueryTextListener OnQueryTextSubmit not starting in empty query string

, ActionBarSherlock onSubmitQuery()

private void onSubmitQuery() {
    CharSequence query = mQueryTextView.getText();
    if (query != null && TextUtils.getTrimmedLength(query) > 0) {
        if (mOnQueryChangeListener == null
                || !mOnQueryChangeListener.onQueryTextSubmit(query.toString())) {
            if (mSearchable != null) {
                launchQuerySearch(KeyEvent.KEYCODE_UNKNOWN, null, query.toString());
                setImeVisibility(false);
            }
            dismissSuggestions();
        }
    }
}

private void onSubmitQuery() {
    CharSequence query = mQueryTextView.getText();
    if(query == null) {query = "";}
    if (mOnQueryChangeListener == null
                || !mOnQueryChangeListener.onQueryTextSubmit(query.toString())) {
        if (mSearchable != null) {
                launchQuerySearch(KeyEvent.KEYCODE_UNKNOWN, null, query.toString());
                setImeVisibility(false);
         }
         dismissSuggestions();
    }
}

, , - .

+5
2

, , EditText, TextWatcher. , .

0

SearchView, ActionBarSherlock, , SearchView, http://novoda.com/blog/styling-the-actionbar-searchview/ OnEditorActionListener EditText ( , enter?), SearchView.

final SearchView searchView = (SearchView)findViewById(R.id.search);
int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
EditText searchPlate = (EditText) searchView.findViewById(searchPlateId);
searchPlate.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            //Do something
        }
        return false;

    });
0

All Articles