Android Surface


Touch event handler for surfaces (e.g. SurfaceView, GLSurfaceView, and others):

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;

public class ExampleClass extends Activity implements View.OnTouchListener{
    public void onCreate(Bundle sis){
        CustomSurfaceView csv = new CustomSurfaceView(this);

    public boolean onTouch(View v, MotionEvent event) {
        // Add a switch (see buttons example) if you handle multiple views
        // here you can see (using MotionEvent event) to see what touch event
        // is being taken. Is the pointer touching or lifted? Is it moving?
        return false;

Or alternatively (in the surface):

public class CustomSurfaceView extends SurfaceView {
    public boolean onTouchEvent(MotionEvent ev) {
        // Handle touch events here. When doing this, you do not need to call a listener.
        // Please note that this listener only applies to the surface it is placed in 
        // (in this case, CustomSurfaceView), which means that anything else which is 
        // pressed outside the SurfaceView is handled by the parts of your app that
        // have a listener in that area.
        return true;