Mr_Hmp
Mr_Hmp

Reputation: 2535

Keyboard is not visible by default in AlertDialog

I have an AlertDialog showing an EditText. It already has the focus (as shown by the orange border around it), however, I have to click on it to show the soft keyboard. Is there any way by which the keyboard is visible when the Dialog is shown?

My Code is:

    final EditText input = new EditText(
                        PatientRegistrationActivity.this);
                input.setLines(1);
                input.setInputType(InputType.TYPE_CLASS_NUMBER);
                AlertDialog.Builder alt_bld = new AlertDialog.Builder(
                        PatientRegistrationActivity.this);
                alt_bld.setTitle(String.valueOf("Enter Age"));
                alt_bld.setView(input);

                alt_bld.setPositiveButton("ok",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                if (input.getText().toString() != "") {
                                    Calendar c = Calendar.getInstance();
                                    int yy = c.get(Calendar.YEAR)
                                            - Integer.valueOf(input.getText()
                                                    .toString());
                                    DOBTv.setText(c.get(Calendar.DATE) + "/"
                                            + (c.get(Calendar.MONTH) + 1) + "/"
                                            + yy);
                                }
                            }
                        });
                alt_bld.setNegativeButton("cancel",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {

                            }
                        });

                final AlertDialog alert = alt_bld.create();
                alert.show();

                alert.getWindow()
                        .setSoftInputMode(
                                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            }
        });

enter image description here

Upvotes: 0

Views: 161

Answers (1)

Chintan Rathod
Chintan Rathod

Reputation: 26034

Try following code which will explicitly call soft keyboard to shown up on screen. Write following code after showing your dialog.

InputMethodManager imm = (InputMethodManager)getSystemService(
            Context.INPUT_METHOD_SERVICE);
imm.showSoftInputFromInputMethod(your_edit_text.getWindowToken(), 0);

Upvotes: 1

Related Questions