Show soft keyboard for dialog Show soft keyboard for dialog android android

Show soft keyboard for dialog


Awesome question, I was trying to do that too and found a solution.

Using the dialog builder class AlertDialog.Builder you will have to invoke the dialog like this:

AlertDialog.Builder builder = new AlertDialog.Builder();AlertDialog dialog;builder.set...dialog = builder.create();dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);dialog.show();

This worked fine for me.

Note: you must import android.view.WindowManager.LayoutParams; for the constant value there.


 AlertDialog dialog = new AlertDialog.Builder(this).create();    dialog.show();    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);


Kotlin

Here's the tested code.

val dialog = AlertDialog.Builder(requireContext()).apply {    setTitle(…)    setView(editText)    setPositiveButton(…)    setNegativeButton(…)}val window = dialog.show().windowwindow?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)

Make sure you access the window property from show() method. Getting window from create() method was returning null for me, so the keyboard wasn't showing.

Import AlertDialog from androidx.appcompat.app.AlertDialog.Import WindowManager from android.view.