Kevin Cruijssen
Kevin Cruijssen

Reputation: 9326

Don't dismiss PopupWindow when clicking outside, only when the Close button is clicked

I have a PopupWindow with a Close-Button, and I only want to close the PopupWindow when this Close-Button is clicked. In addition, I don't want to underlying Activity-View to be affected by any touches.

This has been asked before: Android Popup Window dismisses when clicked outside. But since it's a question and answer of three years ago (2011) I was wondering if there is a better solution for this now or I should indeed use the accepted answer's method.

Upvotes: 2

Views: 6262

Answers (2)

Zain
Zain

Reputation: 40810

You need to do both:

    popupWindow.setOutsideTouchable(false);
    popupWindow.setFocusable(false);

Check this out

Upvotes: 3

i.n.e.f
i.n.e.f

Reputation: 1803

try this link OR one of the below code

pw.setOutsideTouchable(false);

OR

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Rect dialogBounds = new Rect();
getWindow().getDecorView().getHitRect(dialogBounds);

if (!dialogBounds.contains((int) ev.getX(), (int) ev.getY())) {
    // Tapped outside so we finish the activity
    this.finish();
}
return super.dispatchTouchEvent(ev);
}

Hope this helps...

Upvotes: 0

Related Questions