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.
I found this example http://android-codes-examples.blogspot.com/2011/11/show-or-hide-soft-keyboard-on-opening.html. Add the following code just before
alert.show()
.Why this answer - Because above solution will show your keyboard but it will not vanish if you click anywhere other that
EditText
. So you need to do something to make the keybaord disappear whenEditText
loses focus.You can achieve this by doing the following steps:
Make the parent view(content view of your activity) clickable and focusable by adding the following attributes
Implement a hideKeyboard() method
Lastly, set the onFocusChangeListener of your edittext.
For showing keyboard use:
For hiding keyboard use:
I had the same problem and solved it with the following code. I'm not sure how it will behave on a phone with hardware keyboard.
Take a look at this discussion which handles manually hiding and showing the IME. However, my feeling is that if a focused
EditText
is not bringing the IME up it is because you are callingAlertDialog.show()
in yourOnCreate()
or some other method which is evoked before the screen is actually presented. Moving it toOnPostResume()
should fix it in that case I believe.I call this in onCreate() to show keyboard automatically, when I came in the Activity.