title: How to dismiss keyboard when clicking non-EditText views in child fragment
body: <p>So I find this really cool piece of code for dismissing the keyboard when I click non-<code>EditText</code> views. And it works very well, except for Fragments and DialogFragments started using <code>getChildFragmentManager()</code>. Will someone please shed some light on why the exception and how I might fix it?</p>
  
  <pre><code>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 &lt; ((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);
  }
  </code></pre>
  
  <p>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 <code>getChildFragmentManager()</code>, then the code does not affect the views of FragmentB.</p>