Skip to content Skip to sidebar Skip to footer

Android Onkeylistener Not Registering Enter And Search Keys.

I am using a Popup window and within that window I have a search view. Upon clicking the search view the soft keyboard comes on screen. So I want whenever I press the search butto

Solution 1:

You want getKeyCode() not getAction().

Using an IME action listener would be another way to acheive this.

Solution 2:

Use arg2.getAction() instead of getKeyCode() and it should work.

Solution 3:

The searchView has it's on callbacks from the keyboard.

Handle the search/enter in the setOnQueryTextListener. This listener has 2 callbacks:

  1. a onQueryTextSubmit
  2. and a onQueryTextChange

Both pick up the events from your keyboard.

This is part of my code (called in the onCreateOptionsMenu)

MenuItemsearchItem= menu.findItem(R.id.action_search);

    SearchManagersearchManager= (SearchManager) SearchActivity.this.getSystemService(Context.SEARCH_SERVICE);

    if (searchItem != null) {
        finalSearchViewsearchView= (SearchView) searchItem.getActionView();
        if (searchView != null) {
            searchView.setSearchableInfo(searchManager.getSearchableInfo(SearchActivity.this.getComponentName()));
        }

        if (searchView == null) {
            returntrue;
        }

        searchView.setOnQueryTextListener(newSearchView.OnQueryTextListener() {

            @OverridepublicbooleanonQueryTextSubmit(String query) {
            // handle text submitted by user in hereStringtext= query;
                searchView.setQuery("", false);
                InputMethodManagerimm= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
                if (text.length() > 0) downloadSearchQuery(text);
                returntrue;
            }

            @OverridepublicbooleanonQueryTextChange(String newText) { 
            // handle text changed hereStringmQueryString= newText.toString().trim();
                if (mQueryString.toString().trim().length() >= 3) {
                    downloadSearchQuery(mQueryString.toString().trim());
                } else {
                    if (mSearchList.size() != 0) {
                        mSearchList.clear();
                        mAdapter.notifyDataSetChanged();
                    }
                    checkAdapterIfEmpty();
                }
                returntrue;
            }
        });
    }

Post a Comment for "Android Onkeylistener Not Registering Enter And Search Keys."