Kristaps Grinbergs Kristaps Grinbergs - 1 month ago 20
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

onKeyDown
with this code:

public boolean onKeyDown(int key, KeyEvent event) {

if (key == KeyEvent.KEYCODE_SEARCH) {
EditText input = (EditText) findViewById(R.id.list_search_input);
input.setText(null);

input.requestFocus();


// 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.