0

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;
        }
}); 
Michael Celey
  • 12,645
  • 6
  • 57
  • 62
Ravi
  • 2,277
  • 3
  • 22
  • 37

3 Answers3

1

You want getKeyCode() not getAction().

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

alex
  • 6,359
  • 1
  • 23
  • 21
  • Did you try the Action Listener? – alex Feb 21 '13 at 04:47
  • if i am pressing key from hardware keyboard these functions are working but not working with soft keyboard ,please tell me whether this is the problem that i am having edittext or serachview in popup window – Ravi Feb 21 '13 at 06:05
  • Working after setting ime and only accepting enter click on keyboard,thanks – Ravi Feb 22 '13 at 03:53
  • @Ravi - can you send code snipped please with your solution? – Miramax Mars Mar 03 '23 at 10:19
0

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

techieWings
  • 2,045
  • 2
  • 16
  • 18
0

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)

    MenuItem searchItem = menu.findItem(R.id.action_search);

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

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

        if (searchView == null) {
            return true;
        }

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String query) {
            // handle text submitted by user in here
                String text = query;
                searchView.setQuery("", false);
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
                if (text.length() > 0) downloadSearchQuery(text);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) { 
            // handle text changed here
                String mQueryString = newText.toString().trim();
                if (mQueryString.toString().trim().length() >= 3) {
                    downloadSearchQuery(mQueryString.toString().trim());
                } else {
                    if (mSearchList.size() != 0) {
                        mSearchList.clear();
                        mAdapter.notifyDataSetChanged();
                    }
                    checkAdapterIfEmpty();
                }
                return true;
            }
        });
    }