Found the problem.

After looking at the source code of PopupWindow, I found the
TouchInteceptor will only be called if the background drawable is not
null. Here is the snip:
    private void preparePopup(WindowManager.LayoutParams p) {
        if (mBackground != null) {
            //...
            PopupViewContainer popupViewContainer = new
PopupViewContainer(mContext);
            // ...

            mPopupView = popupViewContainer;
        } else {
            mPopupView = mContentView;
        }
            // ...
    }

And the mTouchInterceptor.onTouch will only be called inside the
PopupViewContainer.

So in my code, after I set the background drawable to whatever, (I
used new BitmapDrawable()), the touch event will be intercepted here.

However, I still don't understand why this method only works when
there is a background drawable, here is what's said in doc:
"""
public void setTouchInterceptor (View.OnTouchListener l)

Set a callback for all touch events being dispatched to the popup window.
"""

Regards,
Johnny

On Tue, May 26, 2009 at 3:21 PM, Johnny Lee <[email protected]> wrote:
> Hi all,
>
> I want to intercept the OnTouch event in a PopupWindow, but it failes
> everytime. Here is the code snip:
>
> public class Demo extends Activity {
>
>       �...@override
>        protected void onCreate(Bundle savedInstanceState) {
>                super.onCreate(savedInstanceState);
>
>                // Prepare the popup window
>                final TextView popupText = new TextView(this);
>                popupText.setText("Poping up...");
>                popupText.setBackgroundColor(Color.BLUE);
>                popupText.setFocusableInTouchMode(true);
>
>                final PopupWindow popupWindow = new PopupWindow(this);
>                popupWindow.setContentView(popupText);
>                popupWindow.setWidth(100);
>                popupWindow.setHeight(100);
>                popupWindow.setBackgroundDrawable(null);
>                popupWindow.setOutsideTouchable(true);
>                popupWindow.setTouchInterceptor(new OnTouchListener() {
>
>                        public boolean onTouch(View v, MotionEvent event) {
>                                Log.d("Demo", "popupWindow::onTouch >>> view: 
> " + v + ", event: " + event);
>                                return false;
>                        }
>
>                });
>
>                // Prepare the activity view
>                final LinearLayout layout = new LinearLayout(this);
>                layout.setOrientation(LinearLayout.VERTICAL);
>                layout.setOnTouchListener(new OnTouchListener() {
>
>                        public boolean onTouch(View v, MotionEvent event) {
>                                Log.d("Demo", "layout::onTouch >>> view: " + v 
> + ", event: " + event);
>                                return false;
>                        }
>
>                });
>
>                final TextView backgroundView = new TextView(this);
>                backgroundView.setWidth(240);
>                backgroundView.setHeight(300);
>                backgroundView.setBackgroundColor(Color.RED);
>                layout.addView(backgroundView);
>
>                final Button button = new Button(this);
>                button.setWidth(240);
>                button.setHeight(20);
>                button.setOnClickListener(new OnClickListener() {
>
>                        public void onClick(View v) {
>                                popupWindow.showAsDropDown(backgroundView);
>                        }
>
>                });
>                layout.addView(button);
>
>                setContentView(layout);
>        }
>
> When the activity started, I can click the button to show the popup
> window correctly. But after that, nothing happens when I touch the
> popup window's view. But if I touch any where outside the popup
> window, the log
> Log.d("Demo", "layout::onTouch >>> view: " + v + ", event: " + event);
> will work.
>
> It looks like when I touch the view of the popup window, the touch
> event has been consumed somewhere?
>
> Do you have any ideas on this? Thanks for your consideration.
>
> Regards,
> Johnny
> --
> we all have our crosses to bear
>



-- 
we all have our crosses to bear

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to