40

I'm trying to get a piece of code work which should focus an EditText in an AlertDialog as soon as it shows and then automatically open the soft keyboard. Instead, it just makes the screen go darker.

Builder builder = new Builder(this);
final EditText input = new EditText(this);
AlertDialog dialog = builder.create();
builder
    .setTitle(R.string.dialog_title_addsubject)
    .setMessage(R.string.dialog_addsubject)
    .setView(input)
    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            String value = input.getText().toString();
            if (input.getText().toString().trim().length() == 0) {
                Toast.makeText(Main.this, R.string.input_empty, Toast.LENGTH_SHORT).show();
            } else {
                db.insertSubject(value);
                getData();
            }
         }
    })
    .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    input.requestFocus();
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    dialog.show();

I tried many ways of doing this but none worked. I hope you guys can help me here. Thanks in advance!

Sabre
  • 4,131
  • 5
  • 36
  • 57
NiPfi
  • 1,710
  • 3
  • 18
  • 28

6 Answers6

73

Ok I managed to get it working:

Builder builder = new Builder(this);
            final EditText input = new EditText(this);
            builder
                .setTitle(R.string.dialog_title_addsubject)
                .setMessage(R.string.dialog_addsubject)
                .setView(input)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        String value = input.getText().toString();
                        if (input.getText().toString().trim().length() == 0) {
                            Toast.makeText(Main.this, R.string.input_empty, Toast.LENGTH_SHORT).show();
                        } else {
                            db.insertSubject(value);
                            getData();
                        }
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
                    }
                })
                .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
                    }

                });

                builder.show();
                input.requestFocus();
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

This method doesn't need a dialog so I can use builder.show() to show the dialog and the code provided by Sabre opens the soft keyboard. Another code snippet in each of the buttons closes the soft keyboard automatically.

NiPfi
  • 1,710
  • 3
  • 18
  • 28
  • AWESOME!!! Been trying to get the keyboard to show up with my edittext in alert dialog and nothing worked. thanks so much – gbotha Nov 06 '12 at 02:36
  • Thanks. It helped for me. – S.J. Lim Jan 11 '16 at 06:33
  • This worked, but bare in mind that you must use builder.setView() because if you use dialog.setContentView() the keyboard will appear beneath the Dialog and won't catch inputs. – GuilhE Oct 27 '17 at 16:04
  • 1
    It works, but what if the user doesn't press Positive nor Negative Button, but rather clicks on white space The Keyboard stays on after dialogue is dismissed – smj Nov 03 '18 at 02:36
16

You can use this instead of dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

Call this after dialog.show();

Sabre
  • 4,131
  • 5
  • 36
  • 57
  • 1
    Was my answer useful for you? – Sabre Oct 21 '12 at 18:32
  • Ok this work but I can show the cursor on textView. Any idea? – adev Jan 30 '16 at 19:07
  • Actually, it worked great but with dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); called before dialog.show() – dorsz Aug 22 '16 at 08:34
  • My AlertDialog is in a separate DialogFragment class. Using the `InputMethodManager` method worked on many devices, but not all. Using `alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);` made it work on all for me. – Eric B. Sep 05 '16 at 20:54
  • This fixed my issue, I only needed the first line of code. – 476rick Nov 09 '17 at 16:05
2
   public void selectContact(Context context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(R.mipmap.icon);
        builder.setTitle(R.string.title);
        builder.setPositiveButton(android.R.string.ok, context);
        builder.setNegativeButton(android.R.string.cancel,context);
        builder.setView(View.inflate(context,
                R.layout.dialog, null));
        AlertDialog alertDialog = builder.create();

        alertDialog.setOnShowListener(this); //Add listener
        alertDialog.show();
    }

open keyborad in onShow :-

    @Override
    public void onShow(DialogInterface dialog) {
        EditText editText = (EditText) ((AlertDialog) dialog).findViewById(R.id.number);
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
    }
R_K
  • 803
  • 1
  • 7
  • 18
0

Try showing it after a second -

new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
    input.requestFocus();

    dialog.getWindow().
  setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 

    dialog.show();  
}, 1000)
js123
  • 96
  • 2
  • 8
0

This is what worked for me. Request focus on the edit text and then post a delayed task on it to show the keyboard.

editTextView.requestFocus()
editTextView.postDelayed(200) {
    ContextCompat.getSystemService(requireContext(), InputMethodManager::class.java)
        ?.showSoftInput(
            editTextView,
            InputMethodManager.SHOW_IMPLICIT,
        )
}

Using SHOW_FORCED will keep the keyboard until the user manually closes it, even if they have already dismissed the dialog, so use SHOW_IMPLICIT

Pawan
  • 73
  • 3
  • 10
0

After trying different suggestions, finally clearing flags and setting soft input mode on window works. Make sure to call this after dialog.show():

dialog.window?.apply {
    clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
    clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
    setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
}
Micer
  • 8,731
  • 3
  • 79
  • 73