I have a custom View, which consists of a button, and a view that animates in below the button that contains a custom view of multi-choice items. When the user presses the button, I show the “dropdown” with the items. I want to hide the “dropdown” when they press outside of the dropdown. I tried overriding the onTouchEvent and the onInterceptTouchEvent, but these aren’t always called.
I took a look at the source for the Spinner, and noticed that Google is using an Dialog for what I believe to be its dropdown (how it is being positioned is beyond me at this point).
Is there any way I can have my View intercept ALL touch events on the Window?
You can implement the
onTouchListeneron the topViewGroup(e.g., on aLinearLayoutor whatever you’re using).Then, determine the position of your custom view: If the touch position (using
event.getX()andevent.getY()methods) is outside the View (usingmyView.getTop(), etc.), then it can hide it (myView.setVisibility(View.GONE).In any case, it should
return falseto allow the childrenViews to handle the touch.From the android dev guide: