Eweb
Eweb

Reputation: 71

private dispatchTouchEvent?

I have a class named dot and many instances of this same class are created when the app runs. Problem is I need to be able to click on one of the instances of this class and have the clicked instance only change colors.

The problem is whenever I click on one of the dot instances, all of them change color instead of just the one I clicked.

Here's the code:

    package com.ewebapps;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;

public class Dot extends View {
     private final float x;
     private final float y;
     private final int r;
     private final Paint mBlack = new Paint(Paint.ANTI_ALIAS_FLAG);
     private final Paint mWhite = new Paint(Paint.ANTI_ALIAS_FLAG);
     private final Paint mGreen = new Paint(Paint.ANTI_ALIAS_FLAG);
     private boolean touched;

     public Dot(Context context, float x, float y, int r) {
         super(context);
         mBlack.setColor(0xFF000000); //Black
         mWhite.setColor(0xFFFFFFFF); //White
         mGreen.setColor(0xFF00FF00); //Green
         this.x = x;
         this.y = y;
         this.r = r;
     }

     @Override
  public boolean dispatchTouchEvent(MotionEvent event) { // On touch.
      touched = true;
      //mPaint.setColor(0xFF00FF00); // Turn dot green.
      this.invalidate();
         return super.dispatchTouchEvent(event);
     }

     @Override
     protected void onDraw(Canvas canvas) {
         super.onDraw(canvas);
         canvas.drawCircle(x, y, r+2, mWhite); //White stroke.

         if(!touched)
         {
          canvas.drawCircle(x, y, r, mBlack); //Black circle.
         }
         else
         {
          canvas.drawCircle(x, y, r, mGreen); //Green circle.
         }
     }

}

Upvotes: 1

Views: 4992

Answers (1)

Falmarri
Falmarri

Reputation: 48587

We have no idea how you're declaring or calling or creating these views, so we have no idea how to help you.

The only thing I can say is that dispatchTouchEvent is NOT the same thing as onTouchEvent

It looks like dispatchTouchEvent is called whenever there is ANY touch event on the screen, not on you. So all your views are receiving the touch event, then setting to true.

Called to process touch screen events. You can override this to intercept all touch screen events before they are dispatched to the window. Be sure to call this implementation for touch screen events that should be handled normally.

Upvotes: 2

Related Questions