0

I have been trying to find a solution that works for my specific use case, but I have yet to find the proper setup. I have a custom AlertDialog with an EditText. I am handling the text input without the system keyboard, so having it pop up when I show the dialog on screen is an issue.

I have already looked at a number of different solutions, but none of them have worked for me. This Stack Overflow question as well as this one, this Medium article, and this tutorial.

public void showAlert() {
    alert.show();
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);

    // using this view to hide the keyboard causes a NPE
    View view = ((AppCompatActivity) context).getCurrentFocus();

    // I have also passed in a 0 for this one.
    imm.hideSoftInputFromWindow(ttsAlert.getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);

    // This is the editText in the view
    imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);

    // This is the rootView of the custom layout
    imm.hideSoftInputFromWindow(rootView.getWindowToken(), 0);

    editText.setShowSoftInputOnFocus(false);
}

If anyone can determine what small detail (or large, obvious code block) I have missed to keep the keyboard from showing, I would deeply appreciate it.

Andrea Ebano
  • 523
  • 1
  • 4
  • 15
AshesToAshes
  • 97
  • 1
  • 9
  • I should note, I need the cursor to remain visible. I have found a few more solutions that involve hiding the cursor, but that won't work for my use case. – AshesToAshes Jul 01 '19 at 14:31
  • Test your solution well with Samsung, Motorola, Sony, and LG phones. The former is known for _big f**k ups_ with the Keyboard/Layout engine. – Martin Marconcini Jul 01 '19 at 16:42

1 Answers1

0

After much more digging, I found the solution I needed for my use case. The keyboard does not appear, but the cursor stays.

alert.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

The method is now simply this:

public void showAlert() {
    alert.show();
    alert.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
AshesToAshes
  • 97
  • 1
  • 9
  • I can confirm this solution works for Lenovo tablets. I do not have devices from other companies to test this with. Fortunately for my use case, this is all that matters. – AshesToAshes Jul 03 '19 at 13:55