0

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

Ant4res
  • 1,187
  • 1
  • 15
  • 34
Mr_Hmp
  • 2,316
  • 1
  • 28
  • 49
  • 1
    Try this answers: http://stackoverflow.com/questions/2403632/android-show-soft-keyboard-automatically-when-focus-is-on-an-edittext – Ant4res Jun 24 '13 at 13:27

1 Answers1

1

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);
Chintan Rathod
  • 24,674
  • 13
  • 76
  • 92