1

So I find this really cool piece of code for dismissing the keyboard when I click non-EditText views. And it works very well, except for Fragments and DialogFragments started using getChildFragmentManager(). Will someone please shed some light on why the exception and how I might fix it?

public static void setupUI(View view, final Activity activity) {

    // Set up touch listener for non-text box views to hide keyboard.
    if (!(view instanceof EditText)) {

        view.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                LayoutUtils.hideSoftKeyboard(activity);
                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);

            setupUI(innerView, activity);
        }
    }
}

private static void hideSoftKeyboard(Activity activity) {
    InputMethodManager inputMethodManager = (InputMethodManager) activity
        .getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}

I keep the code in a utility class and use it throughout my app. Essentially, for the problem case, I use it on FragmentB is started by FragmentA using getChildFragmentManager(), then the code does not affect the views of FragmentB.

Blo
  • 11,903
  • 5
  • 45
  • 99
learner
  • 11,490
  • 26
  • 97
  • 169

4 Answers4

0

try this for hide keyboard,

public void hideSoftKeyboard() {
        try {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
Akash Moradiya
  • 3,318
  • 1
  • 14
  • 19
0

try the below code:

@Override
public boolean onTouchEvent(MotionEvent event) {
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    return true;
}
rupesh
  • 2,865
  • 4
  • 24
  • 50
0

If you are using fragment use FragmentActivity not Activity. I suggest use try catch.

like

try{
InputMethodManager inputMethodManager = (InputMethodManager) activity
        .getSystemService(Activity.INPUT_METHOD_SERVICE);
}catch(Exception){
InputMethodManager inputMethodManager = (InputMethodManager) fragmentactivity.getSystemService(Activity.INPUT_METHOD_SERVICE);
}
JayR
  • 441
  • 1
  • 4
  • 16
0

I found the solution. Maybe someone else will find it useful

public static void setupUI(View view, final Activity activity) {

    // Set up touch listener for non-text box views to hide keyboard.
    if (!(view instanceof EditText)) {

        view.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                LayoutUtils.hideSoftKeyboard(activity,v);
                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);

            setupUI(innerView, activity);
        }
    }
}

private static void hideSoftKeyboard(Activity activity, View v) {
    InputMethodManager inputMethodManager = (InputMethodManager) activity
        .getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}

See how I replace activity.getCurrentFocus() with v. And that's it. It works in all situations now.

learner
  • 11,490
  • 26
  • 97
  • 169