title: How to dismiss a popup in Android?
body: <p>I have a Popup window that pops up when a button in an activity is clicked. The dialog covers the whole screen and contains only three buttons. If a button is clicked, then they function accordingly. However, if a user touches anywhere in a popup other than buttons, I want the popup to dismiss. I tried the following code.</p>
  
  <pre><code>popupWindow.setOutsideTouchable(true);
  popupWindow.showAtLocation(layout, Gravity.FILL, 0, 0);
  
  popupWindow.setTouchInterceptor(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
          if (event.getAction() == MotionEvent.ACTION_DOWN) {
              popupWindow.dismiss();
              return true;
          }
          return false;
      }
  });
  </code></pre>
  
  <p>But it didn't work. I also set this:</p>
  
  <pre><code>popupWindow.setFocusable(true);
  
  popupWindow.setTouchable(true);
  </code></pre>
  
  <p>My popup layout looks like this:</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/popup_element"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#80000000"
  android:gravity="center_vertical"
  android:orientation="vertical"
  android:padding="10px"&gt;
  
  &lt;LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:padding="16dp"&gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:orientation="vertical"
          android:padding="10dp"&gt;
  
          &lt;ImageButton
              android:id="@+id/voiceCall"
              android:layout_width="100dp"
              android:layout_height="100dp"
              android:layout_gravity="center|center_horizontal"
              android:background="@drawable/round_button"
              android:padding="15dp"
              android:scaleType="fitCenter"
              android:src="@drawable/ic_call_black_24dp" /&gt;
  
          &lt;TextView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:gravity="center"
              android:text="Voice Call"
              android:textColor="#ffffff"
              android:textSize="20dp" /&gt;
      &lt;/LinearLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/DrTJ4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DrTJ4.png" alt="enter image description here"></a></p>
  
  <p>If I click anywhere on this popup except for those two buttons, I want it to be dismissed.
  Nothing works for me. Can anyone suggest me how to do this? Also, I want the popup to disappear if a device back button is clicked.</p>