2

I am trying to get a simple surface view to respond to touch events. The application below launches but does not respond to touch events. I have a Log.i statement to confirm (by printing to the console) whether or not the touch event is working. Can anyone tell me what I am doing wrong?

This is the my main activity

public class MainActivity extends Activity {

    public static int screenWidth, screenHeight;
    public static boolean running=true;
    public static MainSurface mySurface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //this gets the size of the screen
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        screenWidth = displaymetrics.widthPixels;
        screenHeight = displaymetrics.heightPixels;
        Log.i("MainActivity", Integer.toString(screenWidth) + " " + Integer.toString(screenHeight));

        mySurface = new MainSurface(this);

        setContentView(mySurface);
    }

}

This is the surface view class

public class MainSurface extends SurfaceView implements OnTouchListener {

    public MainSurface(Context context) {
        super(context);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int x = (int)event.getX();
        int y = (int)event.getY();
        int point = event.getPointerCount();
        Log.i("MainSurface", Integer.toString(x)); //nothing prints to the console here
        return true;
    }

}

2 Answers 2

5
  1. Remove implements OnTouchListener.
  2. Change onTouch(View v, MotionEvent event) to onTouchEvent(MotionEvent event).

The reason it's not working is that the SurfaceView doesn't know that it is supposed to be it's own OnTouchListener without you telling it. Alternatively, you could make it work by adding this code to your onCreate():

mySurface = new MainSurface(this);
mySurface.setOnTouchListener(mySurface);

However since SurfaceView already has an OnTouchEvent function, it's simpler to just use that.

Also, don't declare your SurfaceView as static.

2
  • Your edit did it. Thanks. Do you know why it doesn't work as an implementation? Just trying to understand a little better.
    – BPoy
    Commented Mar 11, 2015 at 6:33
  • I'm not sure why the onTouchEvent() way isn't working for you, I just tried it out and it worked for me. the onTouch() way needs setOnTouchListener() as noted.
    – samgak
    Commented Mar 11, 2015 at 6:40
2

You should identify Action in touch Event like:

 @Override
public boolean onTouch(View v, MotionEvent event) {

int action = event.getAction();
switch(action){

case MotionEvent.ACTION_DOWN:
break;

case MotionEvent.ACTION_MOVE:
break;

case MotionEvent.ACTION_UP:

break;

case MotionEvent.ACTION_CANCEL:
break;

case MotionEvent.ACTION_OUTSIDE:
break;
 }
  return true;
}
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.