Ludovic Landry Ludovic Landry - 9 months ago 65
Android Question

How to show soft-keyboard when edittext is focused

I want to automatically show the soft-keyboard when an

is focused (if the device does not have a physical keyboard) and I have two problems:

  1. When my
    is displayed, my
    is focused but the keyboard is not displayed, I need to click again on it to show the keyboard (it should be displayed when my
    is displayed).

  2. And when I click done on the keyboard, the keyboard is dissmissed but the
    stays focused and y don't want (because my edit is done).

To resume, my problem is to have something more like on the iPhone: which keep the keyboard sync with my
state (focused / not focused) and of course does not present a soft-keyboard if there is a physical one.


To force the soft keyboard to appear, you can use

EditText yourEditText= (EditText) findViewById(;
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);

And for removing the focus on EditText, sadly you need to have a dummy View to grab focus.

I hope this helps

To close it you can use

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);