title: Android detect soft keyboard closing
body: <p>I am struggling with this matter for a couple of hours and I cannot find a way to determine if the keyboard is shown or not. <br/><br/>
  I have seen multiple questions and answer about this matter which was depended on changing in height of container layout. so it cannot be possible to detect the opening of keyboard exactly when (for example) an Edittext get the focused and as a result, change in full height layout and its views is inevitable.</p>
  
  <p><strong>Solution 1 :</strong>
  I solved this problem by removing focus from all of <strong>EditText</strong> s and change the visibility of the views which I don't need them to be shown when Edittext get focused (make available space to prevent disorder of views) so I have enough time to remove redundant views.</p>
  
  <p>but it is not possible to detect when the keyboard is asked to be closed to make views visible.</p>
  
  <p>if I use the common method which I mentioned in 2nd paragraph to detect closing of the keyboard it will fail my "solution 1".</p>
  
  <p>the only idea I have now is to detect keyboard closing by monitoring the key which is responsible for keyboard closure. bot i cannot detect it in onKeyDown method.</p>
  
  <p><a href="https://i.stack.imgur.com/wuWae.png" rel="noreferrer"><img src="https://i.stack.imgur.com/wuWae.png" alt="enter image description here"></a></p>
  
  <p>so how can I monitor this key?</p>
  
  <p>any guide and idea will be appreciated. 
  Thanks in advance.</p>
  
  <p><strong>Final Solution :</strong><br/>
  thanks to @Nick Cardoso , my final solution is a combination of using <code>onTouchListener</code> to detect the keyboard openness and Nick's solution of closing.</p>
  
  <p>this is onTouchListener for Edittexts :<br/></p>
  
  <pre><code>    View.OnTouchListener TouchListener = new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
          if (event.getAction() == MotionEvent.ACTION_DOWN)
          {
              Container.getViewTreeObserver().removeOnGlobalLayoutListener(gll);
              recyclerView.setVisibility(View.GONE);
              final Handler handler = new Handler();
              handler.postDelayed(new Runnable() {
                  @Override
                  public void run() {
                      Container.getViewTreeObserver().addOnGlobalLayoutListener(gll);
                  }
              }, 100);
          }
          return false;
      }
  };
  </code></pre>
  
  <p><br/></p>
  
  <pre><code>ViewTreeObserver.OnGlobalLayoutListener gll = new ViewTreeObserver.OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
          Rect measureRect = new Rect();
          Container.getWindowVisibleDisplayFrame(measureRect);
          int keypadHeight = Container.getRootView().getHeight() - measureRect.bottom;
  
          if (keypadHeight &gt; 0) {
              // keyboard is opened
          } else {
              recyclerView.setVisibility(View.VISIBLE);
          }
      }
  };
  </code></pre>
  
  <p>here <code>Container</code> is the root view of the layout.</p>