PopupWindow - disable when clicked

I have a PopupWindow in my activity, my PopupWindow thing still shows, even when I interact with my activity (say, scrolling in my list). I can scroll through the list and PopupWindow still exists.

What I want to achieve is when I touch / scroll / press / etc on a screen that is not PopupWindow, I want to reject PopupWindow. Just like the menu works. If you go beyond the menu, the menu will be rejected.

I tried setOutsideTouchable(true) , but it will not close the window. Thank.

+79
android popupwindow android-menu
Sep 02
source share
14 answers

Try setting setBackgroundDrawable to PopupWindow , which should close the window if you touch it.

+123
02 Sep
source share

I found that none of the answers provided worked for me, other than WareNinja's comments on the accepted answer, and Marcin S. will probably also work. Here is the part that works for me:

 myPopupWindow.setBackgroundDrawable(new BitmapDrawable()); myPopupWindow.setOutsideTouchable(true); 

As an alternative:

 myPopupWindow.setFocusable(true); 

I don’t know what the differences are, but the source code of ListPopupWindow actually uses the latter when the modality is set to true using setModal, so Android developers consider this a viable approach, and this is only one line.

+114
Jul 31 '13 at 15:31
source share

I met the same problems and fixed it as below codes. This works great for me.

  // Closes the popup window when touch outside. mPopupWindow.setOutsideTouchable(true); mPopupWindow.setFocusable(true); // Removes default background. mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 

BTW. Do not use the obsolete BitmapDrawable constructor, use this new ColorDrawable (android.R.color.transparent) to replace the default background.

Good luck @. @

+51
Jun 03 '15 at 2:22
source share

I know this late, but I notice that people still have a popup problem. I decided to write a fully working example in which you can close the pop-up window by touching or clicking it or simply touching the window itself. To do this, create a new PopupWindow class and copy this code:

Popupwindow.class

 public class PopupWindow extends android.widget.PopupWindow { Context ctx; Button btnDismiss; TextView lblText; View popupView; public PopupWindow(Context context) { super(context); ctx = context; popupView = LayoutInflater.from(context).inflate(R.layout.popup, null); setContentView(popupView); btnDismiss = (Button)popupView.findViewById(R.id.btn_dismiss); lblText = (TextView)popupView.findViewById(R.id.text); setHeight(WindowManager.LayoutParams.WRAP_CONTENT); setWidth(WindowManager.LayoutParams.WRAP_CONTENT); // Closes the popup window when touch outside of it - when looses focus setOutsideTouchable(true); setFocusable(true); // Removes default black background setBackgroundDrawable(new BitmapDrawable()); btnDismiss.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { dismiss(); }}); // Closes the popup window when touch it /* this.setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_MOVE) { dismiss(); } return true; } }); */ } // End constructor // Attaches the view to its parent anchor-view at position x and y public void show(View anchor, int x, int y) { showAtLocation(anchor, Gravity.CENTER, x, y); } } 

Now create a popup layout: popup.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="1dp" android:orientation="vertical" android:padding="10dp" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:padding="5dp" android:text="PopupWindow Example" android:textColor="#000000" android:textSize="17sp" android:textStyle="italic" /> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical"> <Button android:id="@+id/btn_dismiss" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Dismiss" android:visibility="gone" /> <TextView android:id="@+id/lbl_dismiss" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text="Touch outside of this box to dismiss" android:textColor="#ffffff" android:textStyle="bold" /> </FrameLayout> 

In your main action, instantiate the PopupWindow class:

 final PopupWindow popupWindow = new PopupWindow(this); popupWindow.show(findViewById(R.id.YOUR_MAIN_LAYOUT), 0, -250); 

where YOUR_MAIN_LAYOUT is the layout of the current activity in which a popup window pops up

+21
Sep 29 '12 at 19:01
source share

Thanks for the answer @LunaKong and confirmation @HourGlass. I do not want to make a duplicate comment, but I want to make it clear and concise.

 // Closes the popup window when touch outside. This method was written informatively in Google docs. mPopupWindow.setOutsideTouchable(true); // Set focus true to prevent a touch event to go to a below view (main layout), which works like a dialog with 'cancel' property => Try it! And you will know what I mean. mPopupWindow.setFocusable(true); // Removes default background. mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 

Mttdat.

+11
Nov 14 '17 at 8:51
source share

Note that to cancel using popupWindow.setOutsideTouchable(true) , you need to make the width and height of the wrap_content as shown below:

 PopupWindow popupWindow = new PopupWindow( G.layoutInflater.inflate(R.layout.lay_dialog_support, null, false), WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); popupWindow.setOutsideTouchable(true); popupWindow.setFocusable(true); popupWindow.showAtLocation(view, Gravity.RIGHT, 0, 0); 
+6
Aug 6 '17 at 7:12
source share

With a ListPopupWindow set the window to display as modal.

 mListPopupWindow.setModal(true); 

Thus, clicking outside the ListPopupWindow reject it.

+5
Aug 05 '14 at 18:22
source share
 mPopWindow.setFocusable(true); 
+3
Sep 27 '13 at 6:11
source share

Set the transparency of the window:

 PopupWindow.getBackground().setAlpha(0); 

After setting the background in the layout. It works great.

+3
Feb 17 '14 at 10:08
source share

@ LunaKong offer works like a charm.

But setting mPopupWindow.setFocusable (false). removes the unnecessary touch needed to make the popup disappear.

For example: Suppose there is a pop-up window visible on the screen and you are about to click a button. So, in this case (if mpopwindow.setFocusable (true)) the first time the button is pressed, popupwindow will be fired. But you need to click again for the button to work. if ** (mpopwindwo.setFocusable (false) ** at the click of a button, reject the pop-up window, and also click the mouse button. I hope it helps.

+2
Sep 25 '15 at 10:07
source share
  popupWindow.setTouchable(true); popupWindow.setFocusable(true); popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0); 

It will disable PopupWindow when you tap / tap the screen. Make sure you set the focus to true before showAtLocation.

+1
Jul 31 '17 at 6:21
source share

In some cases, focusing a pop-up window is undesirable (for example, you may not want it to steal focus from a different view).

An alternative approach is to use a touch interceptor:

 popupWindow.setOutsideTouchable(true); popupWindow.setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { popupWindow.dismiss(); } return false; } }); 
+1
Aug 09 '17 at 10:02 on
source share

Use View popupView to close popupWindow.

 'popupView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { popupWindow.dismiss(); } }); 

'If you use this, you can also set OnClickListener to any button inside the popup

0
Jul 20 '18 at 9:07
source share

You can use isOutsideTouchable OR isFocusable to close the popup when tapped outside

 popupWindow.isOutsideTouchable = true // dismiss popupwindow when touch outside popupWindow.isFocusable = true // dismiss popupwindow when touch outside AND when press back button 

The note

  • Currently, after the test I see, setBackgroundDrawable does not help us close popupwindow

  • If you look at the reject code in PopupWindow ( PopupWindow->PopupDecorView->dispatchKeyEvent and PopupWindow->PopupDecorView->onTouchEvent ). You will see that when you press the back button, they deviate to ACTION_UP and when they touch outside, they deviate to ACTION_UP or ACTION_OUTSIDE

0
Jul 18 '19 at 2:22
source share



All Articles