48

I have a problem with my DialogFragment. So to create my view, I use the method described on the android blog. Here is my DialogFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View myLayout = inflater.inflate(R.layout.dialog_connect, null);

    edit = (EditText) myLayout.findViewById(R.id.password_edit);
    edit.requestFocus();
    getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);

    return myLayout;
}

If I use onCreateView(), it works but I would like create an AlterDialog and to do this, I have the following code :

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder
           .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    callback.onYesConnectClick(edit.getText().toString());
                }
            })
            .setNegativeButton(R.string.refuse, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    callback.onNoConnectClick();
                }
            });

    return builder.create();
}

If I comment the code from onCreateView(), the app works but I can't force the keyboard to be shown and if I uncomment onCreateView(), I get a crash. Here is the stack trace :

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.ProfileActivity_}: android.util.AndroidRuntimeException:     requestFeature() must be called before adding content
AndroidRuntime at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2312)
AndroidRuntime at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2362)
AndroidRuntime at android.app.ActivityThread.access$600(ActivityThread.java:156)
AndroidRuntime at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1250)
AndroidRuntime at android.os.Handler.dispatchMessage(Handler.java:99)
AndroidRuntime at android.os.Looper.loop(Looper.java:137)
AndroidRuntime at android.app.ActivityThread.main(ActivityThread.java:5229)
AndroidRuntime at java.lang.reflect.Method.invokeNative(Native Method)
AndroidRuntime at java.lang.reflect.Method.invoke(Method.java:525)
AndroidRuntime at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
AndroidRuntime at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
AndroidRuntime at dalvik.system.NativeStart.main(Native Method)
AndroidRuntime Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content

So my question ==> Can I use the AlertDialog and show the keyboard when the dialog appears ?

mrroboaat
  • 5,152
  • 7
  • 31
  • 64

5 Answers5

132

override onActivityCreated in your dialogfragment and put getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE); in there

tyczj
  • 66,691
  • 50
  • 172
  • 271
  • 6
    wtf ?! I looked for so much time and the solution was so easy ! Yet, there is one thing I don't understand. I tried to set android:windowSoftInputMode="stateVisible" in the manifest about the activity which is responsible of starting the dialog and it doesn't work ... – mrroboaat Jun 21 '13 at 14:55
  • 1
    Thank you!! There are about a dozen other 'solutions' to this problem on StackOverflow and none of them work for me... but this one does. – josh2112 Jun 17 '14 at 17:28
  • 1
    This seems the most working solutions among all I can find. I am curious why this has to be called in onActivityCreated, instead of onResume()? I really love to hear some explanations. – Sean Jan 31 '15 at 04:18
  • I tried so many things and this is the only thing that worked :O – EpicPandaForce Apr 02 '15 at 15:40
  • I have a DialogFragment which a custom view inflating this library. https://github.com/MiguelCatalan/MaterialSearchView. I could not get the keyboard to show when the MaterialSearchView launches from my DialogFragment. – Adam Hurwitz May 02 '17 at 21:12
  • Use `WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE`. This solution works, but if you want to delay showing a keyboard, it won't work (for instance, `lifecycleScope.launch { delay(300) dialog?.window?.setSoftInputMode(SOFT_INPUT_STATE_VISIBLE) }`. – CoolMind Nov 25 '20 at 14:00
19

The answer of tyczj does not work for me.

The solution was, inside onCreateDialog

Dialog d = builder.create();
d.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
return d;

In the end, the code would be like this

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder
           .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    callback.onYesConnectClick(edit.getText().toString());
                }
            })
            .setNegativeButton(R.string.refuse, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    callback.onNoConnectClick();
                }
            });

    Dialog d = builder.create();
        d.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
return d;
}
Purgarcita
  • 215
  • 2
  • 4
14

Use "SOFT_INPUT_STATE_ALWAYS_VISIBLE" instead of "SOFT_INPUT_STATE_VISIBLE" either in onActivityCreated or onCreateDialog method.

Unheilig
  • 15,690
  • 193
  • 65
  • 96
Garfield
  • 139
  • 2
  • 4
8

Watch out for the setLayout() call if you use it. It took me a while to realize that it may override your window's attributes. After wrapping it into a handler, the accepted solution worked for me.

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

    return dialog;
}

@Override
public void onStart() {
    super.onStart();

    // without a handler, the window sizes itself correctly
    // but the keyboard does not show up
    new Handler().post(new Runnable() {
        @Override
        public void run() {
            getDialog().getWindow().setLayout(DIALOG_WIDTH, DIALOG_HEIGHT);
        }
    });
}
SqueezyMo
  • 1,447
  • 2
  • 19
  • 33
0

If you want to show a keyboard after a short delay, you should use another method. Solutions with onActivityCreated and onCreateDialog work right, but for instant keyboard showing. If you have EditText, you should open the keyboard over it, not over a dialog. See pictures at https://stackoverflow.com/a/65007148/2914140.

fun showKeyboard(view: EditText) {
    val imm = view.context.getSystemService(
        Context.INPUT_METHOD_SERVICE) as InputMethodManager?
    imm?.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}


override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    super.onCreateDialog(savedInstanceState)

    val view = activity?.layoutInflater?.inflate(R.layout.your_layout, null)

    view?.edit_text?.run {
        requestFocus() // Required for showing a keyboard.
        setText("Some text")
        setSelection(text.length)
    }

    val dialogBuilder = MaterialAlertDialogBuilder(requireContext()).apply {
        setView(view)
        // Set message, buttons.
        setCancelable(false)
    }
    val dialog = dialogBuilder.create()

    Handler(Looper.getMainLooper()).postDelayed({
        view?.edit_text?.let { showKeyboard(it) }
    }, 100)
//    Instead of Handler you can use coroutines:
//    lifecycleScope.launch {
//        delay(100)
//        view?.edit_text?.let { showKeyboard(it) }
//    }

    return dialog
}
CoolMind
  • 16,738
  • 10
  • 131
  • 165