2

Is it possible to display automatically the soft-keyboard when the dialog with focused EditText created by AlertDialog.Builder is shown?

I have seen some some discussion about the topic, but I did not find any working solution.

Cristian
  • 198,401
  • 62
  • 356
  • 264
STeN
  • 6,262
  • 22
  • 80
  • 125

2 Answers2

3

There is more than one way to skin a mongoose.

AlertDialog.Builder builder = new AlertDialog.Builder(CurrentActivityName.this);
builder.setTitle(“Title”);
builder.setMessage(“Message”);

etc..

//This is the crucial part 
AlertDialog alertDlg = builder.create();
alertDlg.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

alertDlg.show();

It works good in both phone and tablet

Reference

Community
  • 1
  • 1
Durai Amuthan.H
  • 31,670
  • 10
  • 160
  • 241
1

Solved this

InputMethodManager imm = (InputMethodManager)
            SettingsActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);

    mDialog = mDialogBuilder.create();

    mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

        public void onDismiss(DialogInterface dialog) {
            dismissSoftKeyboard();
        }
    });

    mPinDialog.show();
    showSoftKeyboard();

}

private void showSoftKeyboard() {
    if (imm != null) {
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    }
}

private void dismissSoftKeyboard() {
    if (imm != null) {
        imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
    }
}
mvrck
  • 512
  • 1
  • 4
  • 18