Events of more instances of the same custom View
<p>I'm new with Android.
In my project I have the custom View MyView with the follow code</p>

<pre><code>public class MyView extends View {

    private final Bitmap baseBitmap = BitmapFactory.decodeResource(
            getResources(), R.drawable.myImage);
    private final Matrix matrix;
    private boolean active = true;

    public MyView(Context context, Matrix matrix) {
        super(context);
        this.matrix = matrix;
        this.setFocusable(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);   
        if (active) {
            System.out.println("draw "+this.getId());
            canvas.drawBitmap(baseBitmap, matrix, null);
        } else {
        ...
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            System.out.println("---------&gt;"+this.getId());
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            this.matrix.setTranslate(event.getX()-(baseBitmap.getWidth()/2), event.getY()-(baseBitmap.getHeight()/2));
            this.invalidate();
        } else if (event.getAction() == MotionEvent.ACTION_UP) {

            this.active = false;
        }
        return true;
    }
</code></pre>

<p>In my Activity, I instantiate MyView many times and then add them to the main layout. This is its code:</p>

<pre><code>public class MyActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      Display display = getWindowManager().getDefaultDisplay();
      float cx = display.getWidth() / 2, cy = display.getHeight() / 2;
      int radius = 80;
      double distance = 0, distancePoint = 0;
      final int flags = PathMeasure.POSITION_MATRIX_FLAG
            | PathMeasure.TANGENT_MATRIX_FLAG;
      float length = 0;
      setContentView(R.layout.main);
      RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main_view);
      Path pathCircle = new Path();
      pathCircle.addCircle(cx, cy, radius, Direction.CW);
      PathMeasure meas = new PathMeasure(pathCircle, false);
      int nObject = 10;
      length = meas.getLength();
      distance = length/nObject;
      int i = 0;        
            while(i&lt;nObject){
                Matrix m = new Matrix();
                meas.getMatrix((float)distancePoint, m, flags);
                MyView myView = new MyView(this, m);
                System.out.println(myView.toString());
                myView.setId(i);
                mainLayout.addView(myView,i);
                i++;
                distancePoint = distance*i;
            }
      }                               
}
</code></pre>

<p>At runtime, when I touch any MyView element I always get the last. With "<code>System.out.println("---------&gt;"+this.getId());</code>" I can see that the id of the touched element is always the last, even if I toch the first or any other element.  Actualy, I just can move the last element.<br>
Does anyone know why can't I get the event of the right istance of MyView touched? <br>
(I hope my question is clear)<br>
Thanks</p>

<hr>

<p>I changed the code adding the onMeasure method. I used the code of a tutorial, dimensions are not specific for my image. The views are drawn and the result is the same, unfortunately with the same problem. I post the layout xml too, maybe could be useful.</p>

<pre><code>    public class MyActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      Display display = getWindowManager().getDefaultDisplay();
      float cx = display.getWidth() / 2, cy = display.getHeight() / 2;
      int radius = 80;
      double distance = 0, distancePoint = 0;
      final int flags = PathMeasure.POSITION_MATRIX_FLAG
            | PathMeasure.TANGENT_MATRIX_FLAG;
      float length = 0;
      setContentView(R.layout.main);
      RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main_view);
      Path pathCircle = new Path();
      pathCircle.addCircle(cx, cy, radius, Direction.CW);
      PathMeasure meas = new PathMeasure(pathCircle, false);
      int nObject = 10;
      length = meas.getLength();
      distance = length/nObject;
      int i = 0;        
            while(i&lt;nObject){
                Matrix m = new Matrix();
                meas.getMatrix((float)distancePoint, m, flags);
                MyView myView = new MyView(this, m);
                System.out.println(myView.toString());
                myView.setId(i);
                nt spec = MeasureSpec.makeMeasureSpec(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
                      myView.measure(spec, spec);
                mainLayout.addView(myView,i);
                i++;
                distancePoint = distance*i;
            }
      }                               
}


public class MyView extends View {

    private final Bitmap baseBitmap = BitmapFactory.decodeResource(
            getResources(), R.drawable.myImage);
    private final Matrix matrix;
    private boolean active = true;

    public MyView(Context context, Matrix matrix) {
        super(context);
        this.matrix = matrix;
        this.setFocusable(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);   
        if (active) {
            System.out.println("draw "+this.getId());
            canvas.drawBitmap(baseBitmap, matrix, null);
        } else {
        ...
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            System.out.println("---------&gt;"+this.getId());
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            this.matrix.setTranslate(event.getX()-(baseBitmap.getWidth()/2), event.getY()-(baseBitmap.getHeight()/2));
            this.invalidate();
        } else if (event.getAction() == MotionEvent.ACTION_UP) {

            this.active = false;
        }
        return true;
    }


     @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

            int widthMode = MeasureSpec.getMode(widthMeasureSpec);
            int widthSize = MeasureSpec.getSize(widthMeasureSpec);

            int heightMode = MeasureSpec.getMode(heightMeasureSpec);
            int heightSize = MeasureSpec.getSize(heightMeasureSpec);

            int chosenWidth = chooseDimension(widthMode, widthSize);
            int chosenHeight = chooseDimension(heightMode, heightSize);

            int chosenDimension = Math.min(chosenWidth, chosenHeight);

            setMeasuredDimension(chosenDimension, chosenDimension);
        }

        private int chooseDimension(int mode, int size) {
            if (mode == MeasureSpec.AT_MOST || mode == MeasureSpec.EXACTLY) {
                return size;
            } else { // (mode == MeasureSpec.UNSPECIFIED)
                return getPreferredSize();
            } 
        }

        // in case there is no size specified
        private int getPreferredSize() {
            return 300;
        }
}
</code></pre>

<p>The main.xml:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;RelativeLayout android:id="@+id/main_view"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#FF66FF33"&gt;
  &lt;/RelativeLayout&gt;
</code></pre>
