2

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.

Community
  • 1
  • 1
Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135

2 Answers2

3

You need to do both:

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

Check this out

Zain
  • 37,492
  • 7
  • 60
  • 84
0

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...

Community
  • 1
  • 1
i.n.e.f
  • 1,773
  • 13
  • 22
  • The link: This is the opposite of what I want, since he wants to dismiss when clicking outside. The `setOutsideTouchable(false)`: This helps to ignore onClicks / onTouches of my Activity's View, but it still "dismisses" (set to the background without being able to access it anymore) the PopupWindow. About the last one: How is finished the Activity when I click outside the PopupWindow (the same activity in which this PopupWindow is created and displayed) helping me. :S To clarify: I want to do nothing (ignore all touch events and still display the PopupWindow) when clicking outside the popup. – Kevin Cruijssen Jul 24 '14 at 14:09
  • 1
    `final PopupWindow popUpReject = new PopupWindow(popuplayoutReject, WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT, false); popUpReject.setOutsideTouchable(false);` try this code.. – i.n.e.f Jul 24 '14 at 14:11
  • have you tried this `pw.setOutsideTouchable(false);` ? this is as simple of what you want. It will dismiss all the screen touch expect to the pop up dialog & set popup dismiss to onclick of your close button. – i.n.e.f Jul 24 '14 at 14:13
  • Yeah I did. However, even with `popup.setOutsideTouchable(false)`, the PopupWindow still disappears and I'm also still getting a `InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.`-Warning when I click outside of the PopupWindow. – Kevin Cruijssen Jul 24 '14 at 14:25