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

D: I'm showing an input box using AlertDialog. The EditText inside the dialog itself is automatically focused when I call AlertDialog.show(), but the soft keyboard is not automatically shown.
How do I make the soft keyboard automatically show when the dialog is shown? (and there is no physical/hardware keyboard). Similar to how when I press the Search button to invoke the global search, the soft keyboard is automatically shown.

Test Case #6


File ID: #2418314-5-cc


final AlertDialog alertDialog = alert.create();
etName.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View arg0, boolean hasFocus) {
        if (hasFocus) {
            alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        }
    }
});
alertDialog.show();

  1. **I RETRACT MY COMMENT ABOVE** I found out that if you can not get the focus right, take a look at your XML! If you see the tag * * in there - remove it. It seems like the tag will give focus to the EditText, and then your listener will not be fired as the EditText already has focus.
  2. How do you not do this if the device has a hardware keyboard? Seems like this is annoying for those users.
  3. How would I do it using the AlertDialog.Builder? ...final AlertDialog.Builder alert = new AlertDialog.Builder(Main.this);

Comments Quality
Accurate?:
Precise?:
Concise?:
Useful?: