title: Close popup window without click button
body: <p>I don't want to button for closing pop-up window in Android. I just press the screen than pop-up should close. It works only button click If I click close button then it close but I don't want any button in my popup window I just want click anywhere in the screen then it should be close.</p>
  
  <p><strong>MainActivity.java:</strong></p>
  
  <pre><code>import android.os.Bundle;
  import android.app.Activity;
  import android.content.Context;
  import android.view.LayoutInflater;
  import android.view.Menu;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.view.ViewGroup.LayoutParams;
  import android.widget.Button;
  import android.widget.PopupWindow;
  import android.widget.Toast;
  
  public class MainActivity extends Activity {
  
  private Button btn1, btn2;
  private QuickAction quick;
  private PopupWindow window;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      btn1 = (Button) findViewById(R.id.button1);
      btn2 = (Button) findViewById(R.id.button2);
      btn1.setOnClickListener(btn1_Click);
      btn2.setOnClickListener(btn2_Click);
      ActionItem itm1 = new ActionItem(1, "Item1", null);
      ActionItem itm2 = new ActionItem(2, "Item2", null);
      quick = new QuickAction(this, QuickAction.VERTICAL);
      quick.addActionItem(itm1);
      quick.addActionItem(itm2);
      quick.setOnActionItemClickListener(quick_Clicked);
  }
  
  private OnClickListener btn1_Click = new OnClickListener() {
  
      @Override
      public void onClick(View v) {
          quick.show(v);
      }
  
  };
  
  private OnClickListener btn2_Click = new OnClickListener() {
  
      @Override
      public void onClick(View v) {
           LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.popup, null);
            window = new PopupWindow(layout, 500, 400, true);
            window.showAtLocation(layout,17, 0, 0);
            Button btn = (Button) layout.findViewById(R.id.buttonclose);
            btn.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  window.dismiss();
              }
  
            });
      }
  
  
  };
  
  private QuickAction.OnActionItemClickListener quick_Clicked = new QuickAction.OnActionItemClickListener() {
  
      @Override
      public void onItemClick(QuickAction source, int pos, int actionId) {
          if (actionId == 1) {
              Toast.makeText(MainActivity.this, "Item 1 was clicked", Toast.LENGTH_LONG).show();
          } else if (actionId == 2) {
              Toast.makeText(MainActivity.this, "Item 2 was clicked", Toast.LENGTH_LONG).show();
          }
      }
  };
  }
  </code></pre>
  
  <p>Here this is my popup. What kind of changes is enough for me. Should I changes popup class?</p>
  
  <pre><code>public class PopupWindows {
  protected Context mContext;
  public PopupWindow mWindow;
  protected View mRootView;
  protected Drawable mBackground = null;
  protected WindowManager mWindowManager;
  
  /**
   * Constructor.
   * 
   * @param context Context
   */
  public PopupWindows(Context context) {
      mContext    = context;
      mWindow     = new PopupWindow(context);
      mWindow.setBackgroundDrawable(new BitmapDrawable());
      mWindow.setTouchInterceptor(new OnTouchListener() {
          public boolean onTouch(View v, MotionEvent event) {
              if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                  mWindow.dismiss();
  
                  return true;
              }
  
              return false;
          }
      });
  
      mWindowManager = (WindowManager)     context.getSystemService(Context.WINDOW_SERVICE);
  }
  
  /**
   * On dismiss
   */
  protected void onDismiss() {        
  }
  
  /**
   * On show
   */
  protected void onShow() {       
  }
  
  
  /**
   * On pre show
   */
  protected void preShow() {
      if (mRootView == null) 
          throw new IllegalStateException("setContentView was not called with a view to display.");
  
      onShow();
  
  //  if (mBackground == null) 
  //      mWindow.setBackgroundDrawable(new BitmapDrawable());
  //else 
  //      mWindow.setBackgroundDrawable(new BitmapDrawable());
  
      mWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
      mWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
      //      mWindow.setTouchable(true);
  //  mWindow.setFocusable(true);
  //  mWindow.setOutsideTouchable(true);
  
      mWindow.setContentView(mRootView);
  }
  
  /**
   * Set background drawable.
   * 
   * @param background Background drawable
   */
  public void setBackgroundDrawable(Drawable background) {
      mWindow.setBackgroundDrawable(background);
  }
  
  /**
   * Set content view.
   * 
   * @param root Root view
   */
  public void setContentView(View root) {
      mRootView = root;
  
      mWindow.setContentView(root);
  }
  
  /**
   * Set content view.
   * 
   * @param layoutResID Resource id
   */
  public void setContentView(int layoutResID) {
      LayoutInflater inflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
      setContentView(inflator.inflate(layoutResID, null));
  }
  
  /**
   * Set listener on window dismissed.
   * 
   * @param listener
   */
  public void setOnDismissListener(PopupWindow.OnDismissListener listener) {
      mWindow.setOnDismissListener(listener);  
  }
  
  /**
   * Dismiss the popup window.
   */
  public void dismiss() {
      mWindow.dismiss();
  }
  </code></pre>
  
  <p><strong>Popup.xml:</strong></p>
  
  <p><em>It looks ok ı will delete close button. If ı succes to close without close button for my popupwindow</em></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@color/Black"
  android:gravity="center"
  android:orientation="vertical" &gt;
  
  &lt;ImageView
      android:id="@+id/imageView1"
      android:layout_width="500dp"
      android:layout_height="1000dp"
      android:layout_weight="0.09"
      android:src="@drawable/karisikwaffle" /&gt;
  
  &lt;LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="2dp"
      android:layout_marginBottom="10dp"
      android:layout_marginTop="10dp"
      android:background="#33b5e5"
      android:orientation="vertical" &gt;
  &lt;/LinearLayout&gt;
  
  &lt;LinearLayout
      android:id="@+id/linearLayout1"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_marginBottom="15dp"
      android:gravity="center"
      android:orientation="vertical" &gt;
  
      &lt;TextView
          android:id="@+id/textView2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="This is a popupWindow"
          android:textAppearance="?android:attr/textAppearanceMedium"
          android:textColor="@color/light_gray" /&gt;
  
      &lt;Button
          android:id="@+id/buttonclose"
          android:layout_width="108dp"
          android:layout_height="wrap_content"
          android:text="Close" /&gt;
  &lt;/LinearLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>