0

I'm creating an AlertDialog.Builder at which I'm assigning a EditText to get an input. When it opens the keyboard doesn't automatically shows up. Here is my code:

private void AskForInput(int inputType, String title) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(title);

    final EditText input = new EditText(this);
    input.setInputType(inputType);
    builder.setView(input);

    builder.setPositiveButton(getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            keyNumberValue = input.getText().toString() + "#";
            //TODO: do things...
        }
    });
    builder.setNegativeButton(getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    builder.show();
}

I tried to solve using a suggestion I got from another post link:

AlertDialog alertToShow = builder.create();
alertToShow.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
alertToShow.show();

But still I have to tap on the EditText to open the keyboard. Any idea?

Fábio Nascimento
  • 2,644
  • 1
  • 21
  • 27
Oiproks
  • 764
  • 1
  • 9
  • 34
  • 1
    You tried input.requestFocus() already? – Shermano Jun 14 '19 at 15:06
  • Ok.That worked with `alertToShow`. I just get a warning on `setSoftInputMode` -> NullPointerException. Is not important but I don't like exceptions. Any way to get rid of it? – Oiproks Jun 14 '19 at 15:56
  • 1
    never senn this before, but isn't the case of check if alertToShow.getWindow() != null ? If it not works, post the entire warning if possible! – Shermano Jun 14 '19 at 17:15
  • @Shermano right answer. I had to check. – Oiproks Jun 17 '19 at 06:59

0 Answers0