0

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?

lisovaccaro
  • 32,502
  • 98
  • 258
  • 410
  • try with the solution posted. That will help you. – Pratik Sharma Jan 23 '13 at 18:54
  • You could try adding your logging into ``dispatchKeyEvent`` instead. Just in case some View catches search key on the way. – harism Jan 23 '13 at 19:02
  • 1
    @Liso22 I think from Jellybean and up the harware search button is intercepted by Google Now - so you'll have to use the on screen search button implementation for an easy solution. – A--C Jan 23 '13 at 19:09
  • I found a SO question: http://stackoverflow.com/questions/11931395/jelly-bean-search-key – A--C Jan 23 '13 at 19:14

1 Answers1

1

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

Boris Treukhov
  • 17,493
  • 9
  • 70
  • 91
Pratik Sharma
  • 13,307
  • 5
  • 27
  • 37
  • Keydown is not being fired at all by the search button. I tried the code but it still opens Google Now. – lisovaccaro Jan 23 '13 at 19:00
  • @Liso22 strange I just test code again and placed Toast message after `super.onkeydown` and it works like a charm also it checks for the `KeyEvent.KEYCODE_SEARCH` key event perfectly. – Pratik Sharma Jan 23 '13 at 19:09
  • I don't understand, I'm getting your first toast with all keys except the search key. I'll have to try something else – lisovaccaro Jan 23 '13 at 19:17
  • that's exactly what I was trying when I asked the question! except you used toasts except of logs. As I said they are not detected. I think it's because the search key is intercepted after Jelly Bean as suggested in some comment – lisovaccaro Jan 23 '13 at 19:26
  • @Liso22 yes you are right. I added NOTE and REFERENCE for the same. – Pratik Sharma Jan 23 '13 at 19:36