I am using AlertDialog.Builder in order to create an input box, with EditText as the input method.
Unfortunately, the Soft Keyboard doesn\'t pop, al
I've managed to solve it like this:
Dialog = builder.create();
Dialog.show();
Dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
Dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
A much better solution is given here.
dialog.getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
No workaround. EditText
behaves as expected.
In my case, the SoftInputMode wasn't getting displayed when I set it which was before showing the dialog (after creating it). The below code worked for me where I set the SoftInputMode after showing the dialog.
Kotlin:
val dialog = MaterialAlertDialogBuilder(context) // Other builder code
.create()
dialog.show()
dialog.window?.apply { // After the window is created, get the SoftInputMode
clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
}
Java:
AlertDialog dialog = MaterialAlertDialogBuilder(getContext()) // Other builder code
.create();
dialog.show();
Window window = dialog.getWindow();
if(window != null){ // After the window is created, get the SoftInputMode
window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
I hope this helps anyone who was having the same problem as me.
In my case the only way I was able to show the keyboard when the Dialog was shown was by adding to my DialogFragment
:
@Override
public void onResume() {
super.onResume();
getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
myEditText.requestFocus();
}
Note the SOFT_INPUT_STATE_ALWAYS_VISIBLE instead of SOFT_INPUT_STATE_VISIBLE.
From documentation:
// Visibility state for softInputMode: please always make the soft input
// area visible when this window receives input focus.
int SOFT_INPUT_STATE_ALWAYS_VISIBLE;
When you call showDialog()
to show a Dialog created using AlertDialog
in onCreateDialog()
You should put the code in onPrepareDialog()
:
@Override
protected void onPrepareDialog (int id, Dialog dialog, Bundle args)
{
TextView editText=(TextView) dialog.findViewById(R....);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
});
}
final AlertDialog.Builder alert = new AlertDialog.Builder(context);
final AlertDialog dialog = alert.show();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);