title: Java Android: onTouchListener not working
body: <p>I'm new to writing Apps for Java. I managed to build a little Test-App, with a SurfaceView that's constantly redrawn in a Thread based loop. </p>
  
  <p>Now I managed to work all those Graphical things out, though I wanted to implement an onTouchListener. I did as I was told by Google and wrote it into my main activity implementing OnTouchListener and setting my SurfaceView to the OnTouchListener (directly: <code>Panel.p.setOnTouchListener</code> and indirectly: View <code>view = findViewById(R.id.SurfaceView01);view.setOnTouchListener(this);)</code>. </p>
  
  <p>I got results in neither case though. My App is set to 'Fullscreen' with the XML only containing a reference to the Panel.</p>
  
  <p>I have no idea where the error in that could be, that everything works but the onTouchListener. I'm gonna add all the Files that are relevant to the problem here in hopes someone knows what I did wrong.</p>
  
  <p><strong>XML:</strong></p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent" &gt;
  
  
  &lt;com.example.test.Panel android:id="@+id/SurfaceView01" android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:maxHeight="40dip"&gt;
  &lt;/com.example.test.Panel&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>Main Activity:</strong></p>
  
  <pre><code>//'Constructor'
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
      setContentView(R.layout.activity_main);
      //View view = findViewById(R.id.SurfaceView01);
      //view.setOnTouchListener(this);
      Panel.p.setOnTouchListener(this);
  }
  
  //Called when App's being closed
  @Override
  public void onPause(){
      super.onPause();
      CanvasThread._run = false;
  }
  
  //Touching - Somehow doesn't seem to want to do anything :(
  @Override
  public boolean onTouch (View v, MotionEvent event){
      int x = (int)event.getX();
      int y = (int)event.getY();
      CanvasThread.scene.bird.setDestination(x, y);
      //Pressing
      if(event.getAction()==MotionEvent.ACTION_DOWN){}
      //Releasing
      if(event.getAction()==MotionEvent.ACTION_UP){}
      //Moving
      if(event.getAction()==MotionEvent.ACTION_MOVE){}
      return false;
  }
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
  }
  </code></pre>
  
  <p><strong>Panel (SurfaceView):</strong></p>
  
  <pre><code>CanvasThread canvasthread;
  public static Panel p;
  
  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
  }
  
  
  @Override
  public void surfaceCreated(SurfaceHolder holder) {
      canvasthread.setRunning(true);
      canvasthread.start();
  }
  
  
  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
          boolean retry = true;
          canvasthread.setRunning(false);
          while (retry) {
                  try {
                          canvasthread.join();
                          retry = false;
                  } catch (InterruptedException e) {}
          }
  
  }
  
  
  public Panel(Context context, AttributeSet attrs) {
      super(context, attrs);
      getHolder().addCallback(this);
      canvasthread = new CanvasThread(getHolder(), this);
      setFocusable(true);
      p = this;
  }
  
  
  public void onDraw(Canvas canvas, Bitmap img, int x, int y) {
      Paint paint = new Paint();
      canvas.drawBitmap(img, x, y, null);
  }
  
  public void setBlack(Canvas canvas){
      canvas.drawColor(Color.BLACK);
  }
  
  
  //-----------Image Management----------------------
  public Bitmap chest = BitmapFactory.decodeResource(getResources(), R.drawable.chest);
  public Bitmap stairs = BitmapFactory.decodeResource(getResources(), R.drawable.stairs);
  public Bitmap[][] blueBird = doubleCrop(BitmapFactory.decodeResource(getResources(), R.drawable.bluebird), 4, 2);
  public Bitmap[] tileset = cropIt(BitmapFactory.decodeResource(getResources(), R.drawable.tileset), 10);
  
  public Bitmap[] cropIt(Bitmap set, int length){
      int width = set.getWidth()/length;
      int height = set.getHeight();
      Bitmap[] array = new Bitmap[length];
      for(int c=0; c&lt;length; c++){
          array[c] = Bitmap.createBitmap(set, c*width, 0, width, height);
      }
      return array;
  }
  
  public Bitmap[][] doubleCrop(Bitmap set, int yLength, int xLength){
      int width = set.getWidth()/xLength;
      int height = set.getHeight()/yLength;
      Bitmap[][] array = new Bitmap[yLength][xLength];
      for(int cy=0; cy&lt;yLength; cy++){
          for(int cx=0; cx&lt;xLength; cx++){
              array[cy][cx] = Bitmap.createBitmap(set, cx*width, cy*height, width, height);
          }
      }
      return array;
  }
  </code></pre>
  
  <p><strong>CanvasThread:</strong></p>
  
  <pre><code>    _surfaceHolder = surfaceHolder;
      _panel = panel;
      scene = new Scene(_panel);
  }
  
  public void setRunning(boolean run) {
      _run = run;
  }
  
  @Override
  public void run() {
      Canvas c;
      while (_run) {
          try{Thread.sleep(FRAMEWAIT);}catch(Exception ex){}
          c = null;
          try {
              c = _surfaceHolder.lockCanvas(null);
              synchronized (_surfaceHolder) {
                  scene.circle(_panel, c);
              }
          } finally {
              if (c != null) {
                  _surfaceHolder.unlockCanvasAndPost(c);
              }
          }
      }
  }
  </code></pre>
  
  <p>Some advice is much appreciated. </p>
  
  <p>Thank you very much already.</p>