Reputation: 635
I have on my activity several ImageView's that the user can move around the screen. How can I identify which ImageView is actually selected by the user? In Xcode I can assign a unique Tag to each ImageView and than onTouch get the Tag number. Is there a similar way on Android?
this is Xcode:
UITouch *touch = [[event allTouches] anyObject];
if ([touch view].tag == 901)
{
iTouchObject = 901;
[self.view bringSubviewToFront:imgRojo1];
}
In Android Activity I have so far done: in onCreate I can assign an ID to each ImageView
ImageView selImage = (ImageView)findViewById(R.id.i904);
selImage.setId(904);
and in onTouch.MOVED I can use the ImageView with a directly assigned ID. In this case the image moves smooth below the finger on the screen.
iID = 904;
ImageView selImage = (ImageView)findViewById(iID);
BUT, how can I get the ID from the ImageView that the user has touched?
I have tried to use setOnTouchListener:
ImageView selImage = (ImageView)findViewById(R.id.i904);
selImage.setId(904);
selImage.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
iID = v.getId();
ImageView selImage = (ImageView)findViewById(iID);
int eventaction = event.getAction();
int X = (int)event.getX();
int Y = (int)event.getY();
switch (eventaction ) {
case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball
break;
case MotionEvent.ACTION_MOVE: // touch drag with the ball
// move the balls the same as the finger
selImage.setX(X-25);
selImage.setY(Y-25);
break;
case MotionEvent.ACTION_UP:
// touch drop - just do things here after dropping
break;
}
// redraw the canvas
return true;
}});
By this I can select each ImageView and it moves, BUT, it doesn't move smooth and also the ImageView is duplicated on the screen.
Is it possible to get in the first case in the onTouch.MOVED the View and use it to get the ID with: iID = v.getId();
Upvotes: 0
Views: 1497
Reputation: 635
UPDATE:
I have found a solution, maybe not the best optimized, but great working. Hope that it helps others of you in similar situations:
For each ImageView I am assigning an ID (setID) and setOnTouchListener. Only thing I am doing in setOnTouchListener is to assign to an integer the same number as I have used for the ID. Important is to put RETURN FALSE, this will stop the setOnTouchListener.
ImageView selImage = (ImageView)findViewById(R.id.i904);
selImage.setId(904);
selImage.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
iID = 904;
return false;
}
});
I have put added the following onTouchEvent(). First thing here is to select the ImageView with the ID that was set in the integer before.
public boolean onTouchEvent(MotionEvent event) {
ImageView selImage = (ImageView)findViewById(iID);
int eventaction = event.getAction();
int X = (int)event.getX();
int Y = (int)event.getY();
switch (eventaction ) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
selImage.setX(X-25);
selImage.setY(Y-25);
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
Now I can touch any of the ImageView's and move them around on the screen. Hope it helps!
Upvotes: 1
Reputation: 1036
You can check what id your current view has and compare it with the actual resource Id.
For example:
switch (v.getId()) {
case R.id.image1:
// Do this
break;
case R.id.image2:
// ..
break;
case R.id.image3:
// ..
break;
}
You may also check out this page to read a bit more about drag-and-drop with the ChoiceDragListener
instead.
Upvotes: 2