Search code examples
javaandroidpopupwindowkeyevent

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 button or enter button from keybaord it will get the data from the search view and will show the relevant information. I am using an OnKeyListener to get the key but it is not registering the enter and search key presses.

My code:

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

            if(arg2.getKeyCode() == KeyEvent.KEYCODE_SEARCH || arg2.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                popupwindow.setFocusable(false);
                System.out.println("search pressed");
                Toast.makeText(getApplicationContext(), "Search Pressed", 0).show();    
            }
            return false;
        }
}); 

Solution

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