domiauli domiauli - 1 year ago 111
Android Question

On Touch Method SurfaceView Android doesn't work

I created a SurfaceView (you can see it in the following) and started in from my Main Activity. I overwrote the onTouchEvent method in the SurfaceView and the problem is that the data I want to have logged with Log.d isn't logged, I don't get any Message...
Does anyone have an idea how I can fix this?

My SurfaceView:

import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback {

private float top;
private float left;
private float bottom;
private float right;

private MainThread thread;

MainGamePanel(Context context) {
thread = new MainThread(getHolder(), this);

public void surfaceCreated(SurfaceHolder holder) {

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {


public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
while (retry) {
try {
retry = false;
} catch (Exception e) {

public boolean onTouchEvent(MotionEvent event) {

float x = event.getX();
float y = event.getY();

right = x + 30;
left = x - 30;
top = y - 30;
bottom = y + 30;

Log.d("tag", x + " " + y);

return true;

protected void onDraw(Canvas canvas) {



My Main Activity:

import android.os.Bundle;
import android.os.PersistableBundle;

public class MainActivity extends Activity {

public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(new MainGamePanel(getApplicationContext()));


Answer Source

Try changing "getApplicationContext()" to "this" in following part of code:

setContentView(new MainGamePanel(getApplicationContext()));


setContentView(new MainGamePanel(this));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download