Kristaps Grinbergs Kristaps Grinbergs - 6 months ago 35
Android Question

On HTC Desire with onKeyDown and KEYCODE_SEARCH key showed keyboard disappears immediately after it is showed

I have a HTC Desire phone and want to show set focus to input field and show keyboard when user taps hard search button. Keyboard disappears immediately after it is showed on screen.

I have overridden

with this code:

public boolean onKeyDown(int key, KeyEvent event) {

if (key == KeyEvent.KEYCODE_SEARCH) {
EditText input = (EditText) findViewById(;


// show keyboard
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(input, 0);

return super.onKeyDown(key, event);

Waiting for some ideas, thanks!

Answer Source

Try to replace this

return super.onKeyDown(key, event);

with this:

return true;

I think that it's because you are letting the default (super.onKeyDown(key, event);) event handler to be executed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download