Question

I am using a SearchView widget to search text in a ListView.
When entering text a virtual keyboard opens. I want to repeat the search when the "spyglass key" is pressed.
How can I intercept the virtual keyboard key events?

Note: do not confuse the spyglass key, which is in the virtual keyboard, bottom right, with the spyglass icon in the SearchView widget.

I'll tell you what I tried and therefore I know it does not work:

  • onQueryTextChange() on the SearchView
  • onKeyListener() on the SearchView
  • TextWatcher() on the SearchView
  • dispatchKeyEvent() on the ListView
Was it helpful?

Solution

This is the solution I go for.
I haven't found how to get the KeyEVent for the "spyglass key" (I think it is the same as KEYCODE_ENTER), so What I did is to enable the submit button on the SearchView" and I get a call to onQueryTExtSubmit() each time it is clicked (method of the onQueryTextListener).

    sv = (SearchView) findViewById(R.id.viewer_search);
    sv.setOnQueryTextListener(searchListener);
    sv.setSubmitButtonEnabled(true);
    sv.setIconifiedByDefault(false);

And

private OnQueryTextListener searchListener = new OnQueryTextListener() {

    @Override
    public boolean onQueryTextSubmit(String query) {
        // do my stuff
        return true; 
    }

    . . .
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top