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.

Community
  • 1
  • 1

1 Answers1

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!

Niko Adrianus Yuwono
  • 11,012
  • 8
  • 42
  • 64
  • 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. –  Oct 01 '14 at 02:35
  • 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 – Niko Adrianus Yuwono Oct 01 '14 at 02:40
  • What Type is the Invisible View? Because if it is an EditText, it would still show the Keyboard. –  Oct 01 '14 at 02:45
  • you can just use LinearLayout – Niko Adrianus Yuwono Oct 01 '14 at 02:46
  • 2
    android:focusableInTouchMode="true" in my main layout fixed the issue. –  Oct 01 '14 at 03:03