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.
To show keyboard, for me, I had to do the following
Android TextField : set focus + soft input programmatically
Essentially the solution is the following
Where
ShowKeyboard
isAfter a successful input, I also make sure I hide the keyboard
Put these methods in your Util class and use anywhere.
Kotlin
Java
If anyone is getting:
Try adding context to getSystemService call.
So
This is good sample for you :
Snippets of code from other answers work, but it is not always obvious where to place them in the code, especially if you are using an
AlertDialog.Builder
and followed the official dialog tutorial because it doesn't usefinal AlertDialog ...
oralertDialog.show()
.Is preferable to
Because SOFT_INPUT_STATE_ALWAYS_VISIBLE will hide the keyboard if the focus switches away from the EditText, where SHOW_FORCED will keep the keyboard displayed until it is explicitly dismissed, even if the user returns to the homescreen or displays the recent apps.
Below is working code for an AlertDialog created using a custom layout with an EditText defined in XML. It also sets the keyboard to have a "go" key and allows it to trigger the positive button.
alert_dialog.xml:
AlertDialog.java:
I created nice kotlin-esqe extension functions incase anyone is interested