Dismiss PopupWindow on touch outside popup, without using deprecated constructor

mario595 picture mario595 · Oct 4, 2013 · Viewed 16k times · Source

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);

Answer

Chad Bingham picture Chad Bingham · Feb 25, 2015

What I had to do to get it to work:

popup.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(context, android.R.color.transparent)));
popup.setOutsideTouchable(true);