DialogFragment and force to show keyboard
Asked Answered
G

6

51

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 ?

Globetrotter answered 21/6, 2013 at 14:38 Comment(0)
S
137

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

Stogner answered 21/6, 2013 at 14:41 Comment(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 ...Globetrotter
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.Vins
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.Bolus
I tried so many things and this is the only thing that worked :ORapture
I have a DialogFragment which a custom view inflating this library. github.com/MiguelCatalan/MaterialSearchView. I could not get the keyboard to show when the MaterialSearchView launches from my DialogFragment.Bactericide
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) }.Dissemblance
N
21

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;
}
Neurilemma answered 28/6, 2015 at 11:58 Comment(0)
O
16

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

Octarchy answered 17/5, 2016 at 2:17 Comment(2)
This was the solution for me combined with Purgarcita solution in onCreateDialog at the end.Flogging
@Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (getDialog() != null && getDialog().getWindow() != null) getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); } This workedSepulcher
C
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);
        }
    });
}
Cornish answered 4/5, 2016 at 9:45 Comment(1)
just perfect!. setLayout() was creating the problemDorothi
D
1

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://mcmap.net/q/76763/-how-to-make-android-edittext-expand-vertically-when-full.

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
}
Dissemblance answered 25/11, 2020 at 15:22 Comment(0)
B
0

For some reason nothing worked for me other than this along with myEditText.requestFocus() in onCreateView:

override fun onStart() {
    dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
    super.onStart()
}
Bouldon answered 1/6, 2023 at 17:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.