Android Touch event on screen
37,167
Solution 1
try this code
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return super.onTouchEvent(event);
}
Solution 2
The problem you might have when using the onTouchEvent()
is that because of your view hierarchy where you also have other views on top of your activity, touches on views that override this event (buttons, edittext etc) will not go down to you activity anymore and you will not receive them.
You should use dispatchTouchEvent()
instead as this propagates the other way around, from the activity to your other views and always gets called.
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
// Your code here
return super.dispatchTouchEvent(ev);
}
Related videos on Youtube
Author by
bindal
Updated on July 09, 2022Comments
-
bindal almost 2 years
I want to find out any event when user touches on any screen of an android. I find out touch event for particular activity but not for all screen, please give me a solution.
-
HukeLau_DABA over 9 yearsi noticed if you have a more specific touch event like touching an edittext that still works, so this works great!
-
Gene Bo about 9 yearsThis got me on the right track - so thanks! However - using this, a single touch is registered twice in that method. Add this with the logic you want on down touch only: if (event.getAction() == MotionEvent.ACTION_DOWN) { // your stuff ; } .. from this post: stackoverflow.com/a/9548834/2162226
-
isha almost 8 yearsthis solution handle touch event of entire application