title: Hide to show and hide keyboard in DialogFragment
body: <p>In my dialog fragment, I am able to show the keyboard using </p>
  
  <pre><code>getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT STATE_VISIBLE);
  </code></pre>
  
  <p>but I am not able to hide it on <code>dismiss</code>. </p>
  
  <p>I've tried </p>
  
  <pre><code>getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
  </code></pre>
  
  <p>and </p>
  
  <pre><code>getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
  </code></pre>
  
  <p>neither of which work.</p>
  
  <p>I've also tried showing and hiding the keyboard using </p>
  
  <pre><code>InputMethodManager inputManager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
          inputManager.toggleSoftInput(0, 0); 
  </code></pre>
  
  <p>and         </p>
  
  <pre><code>InputMethodManager inputManager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
          inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
  </code></pre>
  
  <p>but these are not able to show or hide the keyboard.</p>
  
  <pre><code> public static class MyDialogFragment extends DialogFragment
      {
          @Nullable @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
          {
              return inflater.inflate(R.layout.my_input_dialog, container, false);
          }
  
          @Override
          public void onViewCreated(View v, Bundle savedInstanceState)
          {
              super.onViewCreated(v, savedInstanceState);
  
              final EditText editText = (EditText)v.findViewById(R.id.input);
              // this line below is able to show the keyboard 
              getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 
              Button add = (Button)v.findViewById(R.id.add_btn);
              add.setOnClickListener(new View.OnClickListener()
              {
                  @Override
                  public void onClick(View v)
                  {
                      // other code not shown
                      dismiss();
                  }
              });
              Button cancel = (Button)v.findViewById(R.id.cancel_btn);
              cancelButton.setOnClickListener(new View.OnClickListener()
              {
                  @Override
                  public void onClick(View v)
                  {
                      dismiss();
                  }
              });
          }
  
          @Override
          public void onDismiss(DialogInterface dialog)
          {
              //this line below does NOT work, it does not hide the keyboard
              getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
              super.onDismiss(dialog);
          }
      }
  </code></pre>
  
  <p><strong>Note</strong>: I have read these stackoverflow posts and have tried the proposed solutions to no avail:</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/16269466/how-to-hide-the-onscreen-keyboard-when-a-dialogfragment-is-canceled-by-the-setca">How to hide the onscreen keyboard when a DialogFragment is canceled by the setCanceledOnTouchOutside event</a></li>
  <li><a href="https://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard">Close/hide the Android Soft Keyboard</a></li>
  </ol>