2

Possible Duplicate:
How to dismiss the dialog with click on outside of the dialog?

How can I click outside of a popup to dismiss it?

Here is my code:

cell.setOnClickListener(new OnClickListener(){

    /*This code is in a separate class so I needed to use ctx as context 
    *and the string "layout_inflater" because it was not recognizing 
    *LAYOUT_INFLATER_SERVICE*/

    @Override
    public void onClick(View arg0) {LayoutInflater layoutInflater  = 
    (LayoutInflater)ctx.getSystemService("layout_inflater");  
    View popupView = layoutInflater.inflate(R.layout.popup_window, null); 
    final PopupWindow popupWindow = new PopupWindow(popupView, 
          LayoutParams.WRAP_CONTENT,  LayoutParams.WRAP_CONTENT);
    popupWindow.showAtLocation(newParentLayout, Gravity.CENTER, 0, 0); 
    }

I've also tried adding all of this but no results.

 popupWindow.setTouchable(true);
 popupWindow.setFocusable(true);
 popupWindow.setOutsideTouchable(true);
 Drawable bg = ctx.getResources().getDrawable(R.drawable.popup_bg);
 popupWindow.setBackgroundDrawable(bg);

I'm out of ideas. Any help?

Edit to add: the main layout is a ViewPager/PagerAdapter if that would affect anything?

Community
  • 1
  • 1
Terence Chow
  • 10,755
  • 24
  • 78
  • 141

2 Answers2

10

Please set setOutsideTouchable(true) along with the background. That worked fine for me. I know that setting the background drawable to null kills the OnTouchListener.

Marcin S.
  • 11,161
  • 6
  • 50
  • 63
  • @Marcin_S can you provide the code for the setbackgrounddrawable? because I've done it above and it did not work. It looks to me that the solution provided in other stackoverflow answers uses a deprecated setBackgroundDrawable and I'd much rather learn the correct way – Terence Chow Sep 23 '12 at 18:25
  • @Marcin_S found my error, had the showAtLocation before setting outside touchable. This is the correct answer. thanks! – Terence Chow Sep 23 '12 at 21:12
  • You are welcome. I'm glad that it works for you. – Marcin S. Sep 23 '12 at 21:34
  • The `setBackgroundDrawable` is the key here ! this is the only answer that mentions it. Thanks – Muzikant Feb 07 '13 at 14:16
  • And make sure to do so before showing it! – Saba Jamalian Jul 04 '14 at 16:39
  • 1
    Hello, I have more than one layout in my activity and when I click outside popup window it is not disabled i have also set BackgrounDrawable and outsideTouchable. – Mohammed Rampurawala Aug 21 '14 at 11:37
0

try this way.

 cell.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            LayoutInflater layoutInflater  = 
                (LayoutInflater)ctx.getSystemService("layout_inflater");  
                View popupView = layoutInflater.inflate(R.layout.popup_window, null); 
                final PopupWindow popupWindow = new PopupWindow(ctx);

                popupWindow.setFocusable(true);
                popupWindow.setContentView(popupView);
                popupWindow.showAtLocation(b, Gravity.CENTER, 0, 0); 
                popupWindow.update(100, 100, 150, 150);
        }
    });
ashakirov
  • 12,112
  • 6
  • 40
  • 40