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
This problem occurs when EditText is added after AlertDialog.onCreate is called.
https://developer.android.com/reference/androidx/appcompat/app/AlertDialog.Builder
The AlertDialog class takes care of automatically setting android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM for you based on whether any views in the dialog return true from View.onCheckIsTextEditor().
You need to clear the FLAG_ALT_FOCUSABLE_IM flag.
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
Because AlertDialog.show is called in the DialogFragment.onStart, you can insert the code in the DialogFragment.onStart.
@Override
public void onStart() {
super.onStart();
getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}
Or you can use the Dialog.setOnShowListener if you do not use a DialogFragment.
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}
});
Window window = dialog.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
I've made such a thing
AlertDialog.Builder b = new AlertDialog.Builder(this);//....
AlertDialog dialog = b.create();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog.show();
This was answered here already. Using an OnFocusChangeListener worked for me.
Try this, its working for me
If you want to display soft keyboard:
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(input.getWindowToken(), 0);
And if you want to hide the it:
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
I found out that the same code works properly on Tablet, the keyboard does pop up, but on Phone it doesn't, so researching further, seems to point to the "adjust" option.
I am using this, feels much cleaner.
AlertDialog d = builder.create();
d.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
d.show();