1

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.

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

5 Comments

I made a View, And set Its visibility to Gone. And then set the Focus to it. However, the Keyboard Still shows every time the dialog is opened.
I guess instead of set visibility gone you could set the width and height to 0dp or 1dp and then set the visibility to invisible because if you set it to Gone, it will be really gone from the layout so it won't catch the focus
What Type is the Invisible View? Because if it is an EditText, it would still show the Keyboard.
you can just use LinearLayout
android:focusableInTouchMode="true" in my main layout fixed the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.