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.
This is bit tricky. I did in this way and it worked.
1.At first call to hide the soft Input from the window. This will hide the soft input if the soft keyboard is visible or do nothing if it is not.
2.Show your dialog
3.Then simply call to toggle soft input.
code:
You can request a soft keyboard right after creating the dialog (test on SDK - r20)
Let me point some additional info to the solution of yuku, because I found it hard to get this working! How do I get the AlertDialog object from my AlertDialog.Builder? Well, it's the result of my
alert.show()
execution:The original question concerns Dialogs and my EditText is on a regular view. Anyhow, I suspect this should work for most of you too. So here's what works for me (the above suggested highest rated method did nothing for me). Here's a custom EditView that does this (subclassing is not necessary, but I found it convenient for my purposes as I wanted to also grab the focus when the view becomes visible).
This is actually largely the same as the tidbecks answer. I actually didn't notice his answer at all as it had zero up votes. Then I was about to just comment his post, but it would have been too long, so I ended doing this post anyways. tidbeck points out that he's unsure how it works with devices having keyboards. I can confirm that the behaviour seems to be exactly the same in either case. That being such that on portrait mode the software keyboard gets popped up and on landscape it doesn't. Having the physical keyboard slid out or not makes no difference on my phone.
Because, I personally found the behaviour a bit awkward I opted for using:
InputMethodManager.SHOW_FORCED
. This works as I wanted it to work. The keyboard becomes visible regardless of the orientation, however, at least on my device it doesn't pop up if the hardware keyboard has been slid out.You can create a focus listener on the
EditText
on theAlertDialog
, then get theAlertDialog
'sWindow
. From there you can make the soft keyboard show by callingsetSoftInputMode
.The problem seems to be that since the place where you enter text is hidden initially (or nested or something), AlertDialog is automatically setting the flag
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
orWindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
so that things don't trigger a soft input to show up.The way that to fix this is to add the following: