Android Soft Keyboard for Dialogs

It seems like such a simple thing, you would like to show the Soft Keyboard automatically for a Dialog. You can easily find a number of ways to do this for an Activity. You can also find one often repeated, but inadequate, method being proliferated for a Dialog

1
2
3
InputMethodManager imm =
(InputMethodManager) getSystemServer(INPUT_METHOD_SERVICE);
imm.toggleSoftInput(imm.SHOW_FORCED, imm.HIDE_NOT_ALWAYS);

There are, however, several problems with this method. It does not…

  • respect the user’s phone and presence of a physical keyboard
  • close automatically when the dialog closes
  • close automatically if the user were to leave the activity or application completely

There is however, a solution if you are targeting at least API Level 3 (Android 1.5). In your onPrepareDialog(), or wherever you are displaying your dialog do the following:

1
2
dialog.getWindow()
   .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

This will have the same result on a Dialog as setting android:windowSoftInputMode="stateVisible" for an activity in your AndroidManifest.xml

Copyright © 2013 Thomas Holmes