DialogFragment and force to show keyboard

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;
}

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


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