I am developing an android app which tracks touch events for the whole application. For that purpose, I want to override onTouchEvent() without an activity; i.e., inside a simple Java class, I don’t whether it’s possible. Any ideas are welcome.
I am developing an android app which tracks touch events for the whole application.
Share
No, you cannot arbitrarily override
onTouchEvent, particularly in a “simple Java class”, i.e. one that does not extend aView.Solution 1
If you want to capture all touches to your app, override
onInterceptTouchEvent()in your main layout. So, if your main layout is aLinearLayout, do something like:Then, in your XML, you’d use:
Solution 2
This is by far the simpler solution, though I’ve had varying luck with it in the past and would never suggest that you reply upon it.
You can simply get the
Viewthat contains all your content (the rootView, aLinearLayoutabove), then give it a listener.