Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismiss PopupWindow on touch outside popup, without using deprecated constructor

I have a PopupWindow and I wanted it to dismiss when the user touches outside, so I looked into and found out that I had to use popup.setBackgroundDrawable(new BitmapDrawable());. The problem is that the constructor new BitmpaDrawable() is deprecated. I Would like to find a solution without using it.

Anybody knows how to solve this?

Thanks!

                final PopupWindow popup = new PopupWindow(sortByView,
                                          ViewGroup.LayoutParams.WRAP_CONTENT,
                                          ViewGroup.LayoutParams.WRAP_CONTENT,            
                                          true);
                popup.setBackgroundDrawable(new BitmapDrawable());
                popup.setOutsideTouchable(true);
                popup.showAsDropDown(v);
like image 787
mario595 Avatar asked Oct 04 '13 09:10

mario595


2 Answers

What I had to do to get it to work:

popup.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(context, android.R.color.transparent)));
popup.setOutsideTouchable(true);
like image 79
Chad Bingham Avatar answered Oct 14 '22 20:10

Chad Bingham


Hmm setBackgroundDrawable don't dissmiss popup window. I think that default behavior of popup window is to dismiss on touching outside but you may add onDismiss listener like that

popup.setOnDismissListener(new PopupWindow.OnDismissListener() {

    @Override
    public void onDismiss() {
        popup.dismiss();
        // end may TODO anything else                   
    }
});
like image 38
Proxain Avatar answered Oct 14 '22 20:10

Proxain