user3011902
user3011902

Reputation:

Android Hide Soft-Keyboard on Dialog with EditText

I have a Custom Dialog Box that contains an EditText. Now, Whenever I show the Dialog usingDialog.show();, the EditText immediately grabs focus and displays the Soft-Keyboard. I attempted to add this to the manifest:

android:windowSoftInputMode="stateHidden"

Based on this answer: https://stackoverflow.com/a/2611031/3011902

I also tried the following on the EditText:

EditText.setSelected(false);

And:

LinearLayout hidden = (LinearLayout) loginDialog.findViewById(R.id.hidden);
hidden.setVisibility(View.INVISIBLE);
hidden.setFocusable(true);
hidden.requestFocus();
loginDialog.show();

I also tried to manually hide the keyboard right after the dialog is shown, but that feels a bit illegitimate. Is there any easy way to only display the keyboard when the EditText of the Dialog is selected.

Upvotes: 1

Views: 1149

Answers (1)

Niko Adrianus Yuwono
Niko Adrianus Yuwono

Reputation: 11112

You can try redirecting the focus to another view or just invisible view inside your Custom Dialog Box with adding
android:focusable="true" and android:focusableInTouchMode="true" or
setFocusable(true) and setFocusableInTouchMode(true)

If you have any question about my answer feel free to ask in the comment!

Upvotes: 1

Related Questions