Touch event handler for surfaces (e.g. SurfaceView, GLSurfaceView, and others):
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
public class ExampleClass extends Activity implements View.OnTouchListener{
@Override
public void onCreate(Bundle sis){
super.onCreate(sis);
CustomSurfaceView csv = new CustomSurfaceView(this);
csv.setOnTouchListener(this);
setContentView(csv);
}
@Override
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 {
@Override
public boolean onTouchEvent(MotionEvent ev) {
super.onTouchEvent(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;
}
}