0

I was wondering, in a Dialog with multiple EditTexts, is there any way to hide the soft keyboard if the user clicks outside any one of them? This dialog has a LOT of EditTexts, and I would like an easy way for the user to hide the soft keyboard.

Since the EditTexts are in the form of a table, I prefer not to use imeOptions="actionDone" for every single EditText, since there are times when the user needs to enter data in an entire column. However, if the user only needs to enter partial data, I would like it where the user can click outside of any EditText and hide the soft keyboard.

I have looked up several solutions here, but none of them seem to work for my scenario. They only seem to work in an activity.

Pink Jazz
  • 804
  • 4
  • 11
  • 29

3 Answers3

0

have you tried setting OnTouchListener for ViewGroup parent of all these EditTexts? and when MotionEvent is not consumed by any of them (method returns true/false) then run your method hideKeyboard();? (some examples HERE)

also you wrote

and I would like an easy way for the user to hide the soft keyboard.

isn't Back physical/on-screen button intended to do that? Always accessible in same place on the front of device, the easiest way... (for usual devices, maybe you have some custom...) But I admit that touching outside any EditText closing keyboard is UX-friendly

Community
  • 1
  • 1
snachmsm
  • 10,975
  • 1
  • 22
  • 52
0

Okay, it looks like setting the root and child container views to clickable, focusable, and focusableInTouchMode to true did the trick, along with the following code:

public void hideKeyboard(View view) {
    InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

public void setupDialog(View view) {
    // Set up touch listener for non-text box views to hide keyboard.
    if (!(view instanceof EditText)) {
        view.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent event) {
                hideKeyboard(view);
                return false;
            }
        });
    }

    //If a layout container, iterate over children and seed recursion.
    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            View innerView = ((ViewGroup) view).getChildAt(i);
            setupDialog(innerView);
        }
    }
}

Where you pass in the id of the root view of the dialog.

Pink Jazz
  • 804
  • 4
  • 11
  • 29
-1

No user clicks outside of a EditText to escape from it. You should better use "actionGo" or "actionDone" for your input fields.

But if you really-really need it, I recommend using onFocusChangeListener. Use this code to hide keyboard:

InputMethodManager imm = (InputMethodManager) getSystemService(
INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

Then check you have this for your EditText:

android:clickable="true" 
android:focusableInTouchMode="true"