0

I use PopupWindow in my project. And I can not dismiss it when touch display. I tried:

@Override
    public void onResume() {
        super.onResume();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                View popup = LayoutInflater.from(getActivity()).inflate(R.layout.popup, null, false);
                final PopupWindow popupWindow = new PopupWindow(popup, (int) DpiToPixel.calculateDpToPixel(150, getActivity()),
                        (int) DpiToPixel.calculateDpToPixel(150, getActivity()), true);
                popupWindow.showAtLocation(rootView, Gravity.CENTER, 0, 0);
                popup.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        popupWindow.dismiss();
                    }
                });
                popupWindow.setOutsideTouchable(true);
                popupWindow.setTouchable(true);
                popupWindow.setBackgroundDrawable(new BitmapDrawable());
                popupWindow.setTouchInterceptor(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        Toast.makeText(getActivity(),"Click!",Toast.LENGTH_LONG).show();
                        popupWindow.dismiss();
                        return true;
                    }
                });
            }
        }, 100L);
    }

But it's not working. I tried popupWindow.setFocusable(true); I tried setBackgroundDrawable on PopupWindow all not work. How can I dismiss this popup????????????

Pankaj
  • 7,908
  • 6
  • 42
  • 65
ip696
  • 6,574
  • 12
  • 65
  • 128

2 Answers2

0

Set a BackgroundDrawable and then set setOutsideTouchable parameter to true.

setOutsideTouchable: Indicates whether the popup window will be informed of touch events outside of its window.

popupWindow.setBackgroundDrawable(new ColorDrawable());
myPopupWindow.setOutsideTouchable(true);

Or try this:

myPopupWindow.setFocusable(true);
heloisasim
  • 4,956
  • 2
  • 27
  • 36
0

Move this statement to the end of your run() method:

popupWindow.showAtLocation(rootView, Gravity.CENTER, 0, 0);

In other words, don't show the popup until after you have made all the calls configuring it.

Bob Snyder
  • 37,759
  • 6
  • 111
  • 158