Android: show soft keyboard automatically when focus is on an EditText Android: show soft keyboard automatically when focus is on an EditText android android

Android: show soft keyboard automatically when focus is on an EditText


You can create a focus listener on the EditText on the AlertDialog, then get the AlertDialog's Window. From there you can make the soft keyboard show by calling setSoftInputMode.

final AlertDialog dialog = ...;editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {    @Override    public void onFocusChange(View v, boolean hasFocus) {        if (hasFocus) {            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);        }    }});


For showing keyboard use:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);

For hiding keyboard use:

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


You can request a soft keyboard right after creating the dialog (test on SDK - r20)

// create dialogfinal AlertDialog dialog = ...; // request keyboard   dialog.getWindow().setSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);