2

Given a PopupWindow defined like this:

public class MyWindow extends PopupWindow implements View.OnTouchListener {
    MyWindow(View view) {
        super(view);

        setHeight(view.getMeasuredHeight());
        setFocusable(true);
        setTouchable(true);
        setTouchInterceptor(this);
    }

    public boolean onTouch(View v, MotionEvent event) {
        System.out.println("onTouch()");
        return true;
    }
}

for some reason, onTouch() is never called.

What am I doing wrong? How can I get the PopupWindow to accept touch events?

Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109

1 Answers1

2

As discussed in this answer to a different question, the PopupWindow needs to have a background Drawable explicitly set, even when it has been inflated from XML and doesn't visually need a background set.

I fixed this by adding this line:

        setBackgroundDrawable(new ShapeDrawable());

to the constructor.

Community
  • 1
  • 1
Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
  • 1
    Great, you solved a huge issue facing me, all the answers i found before were taking about setting the `BackgroundDrawable` as `new BitmapDrawable()` and this method is deprecated now and you are not allowed to set a `BitmapDrawable()` without defining `Resources` and an actual `Bitmap` or at least an `InputStream` that contains a `Bitmap` – Muhammed Refaat May 22 '14 at 08:59