User Touch Handling On Sprite In Android

In this example we will implement onTouchEvent() to handle user touch on SurfaceView. When user touch on the screen(SurfaceView), it will call setX(x) and setY(y) of mySprite object to update its position.


1.) Create a new project by File-> New -> Android Project name it UserTouchOnSurfaceView.

2.) Write following into main.xml:




3.) Create and write following into



4.) Create and write following into




5.) Create and write following into




6.) Create and write following into




7.) Write following permissions to your manifest



8.) Run for output.


1.) Create a project named UserTouchOnSurfaceView and set the information as stated in the image.

Build Target: Android 4.4
Application Name: UserTouchOnSurfaceView
Package Name: com.example.UserTouchOnSurfaceView
Activity Name: UserTouchOnSurfaceViewActivity


2.) Open file and write following code there:




3.) Compile and build the project.