lisovaccaro
lisovaccaro

Reputation: 33956

How to listen for search key?

I'm trying to listen for the search key, I've found two solutions on stackoverflow but neither works with the search button for some reason.

@Override
public boolean onSearchRequested() {
     System.out.println("KEYDOWN");
     return true;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
     System.out.println("KEYDOWN");
     return true;
}

As you can see the first one just the search intent and the second option should catch all keydowns regardless the key, onKeyDown is working correctly for all keys (back, menu and volume keys in my phone) in my Nexus S but simply doesn't catch the search key. People seem to be pretty sure any of this options should work there is even an approved where onSearchRequested is suggested but I have tried in two phones and in neither of them works.

How can I catch and disable the search key on android?

Upvotes: 0

Views: 220

Answers (1)

Pratik Sharma
Pratik Sharma

Reputation: 13415

Try this :

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Toast.makeText(getApplicationContext(), "Fired", Toast.LENGTH_LONG).show();
        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
            Toast.makeText(getApplicationContext(), "Search Key Fired", Toast.LENGTH_LONG).show();
            return true;
        }
        return super.onKeyDown(keyCode, event);
     }

OR :

    @Override
    public boolean onSearchRequested() {
        Toast.makeText(getApplicationContext(), "onSearchRequested", Toast.LENGTH_LONG).show();
        return false;
    }

NOTE :

If your device does enjoy the latest and greatest OS, go ahead and tap that Google Search bar, and you’ll be greeted by the “Get Google Now!” screen (Figure A). At this point, you can choose to enable Google Now or dismiss the feature for later. You can also tap “Learn more” to find out what Google Now offers.

Reference :

Google Now on Android Jelly Bean is more than just a search feature

Thanks

Upvotes: 1

Related Questions