NiPfi
NiPfi

Reputation: 1720

AlertDialog with EditText, open soft keyboard automatically with focus on EditText doesn't work

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!

Upvotes: 40

Views: 31344

Answers (6)

Micer
Micer

Reputation: 8979

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

Upvotes: 0

Pawan
Pawan

Reputation: 73

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

Upvotes: 0

R_K
R_K

Reputation: 1023

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

Upvotes: 2

js123
js123

Reputation: 96

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)

Upvotes: 0

NiPfi
NiPfi

Reputation: 1720

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.

Upvotes: 73

Sabre
Sabre

Reputation: 4183

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();

Upvotes: 17

Related Questions