Reputation: 115
I used onDragListener to drag an object on the screen, and this part works well. But i need also to check the x,y coordinates of the screen while dragging. I override onTouch method and it works well also. but once i dragged the object the ontouch listener doesn't work. I can't make the two listener work together. I don't why it doesn't work together.
the following code for draginng
ima.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ClipData.Item item = new ClipData.Item((CharSequence)v.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData(v.getTag().toString(),
mimeTypes, item);
// Instantiates the drag shadow builder.
View.DragShadowBuilder myShadow = new DragShadowBuilder(ima);
// Starts the drag
v.startDrag(dragData, // the data to be dragged
myShadow, // the drag shadow builder
null, // no need to use local data
0 // flags (not currently used, set to 0)
);
return true;
}
});
// Create and set the drag event listener for the View
ima.setOnDragListener( new OnDragListener(){
@Override
public boolean onDrag(View v, DragEvent event){
switch(event.getAction())
{
case DragEvent.ACTION_DRAG_STARTED:
System.out.println("Action is DragEvent.ACTION_DRAG_STARTED");
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENTERED");
break;
case DragEvent.ACTION_DRAG_EXITED :
Log.d(msg, "Action is DragEvent.ACTION_DRAG_EXITED");
break;
case DragEvent.ACTION_DRAG_LOCATION :
Log.d(msg, "Action is DragEvent.ACTION_DRAG_LOCATION");
right=ima.getRight();
left=ima.getLeft();
top=ima.getTop();
bottom=ima.getBottom();
System.out.println("Start Touch "+right+" "+top+" "+left+" "+bottom);
/* if(x_cord>left&&y_cord>top&&x_cord<right&&y_cord<bottom){
System.out.println("GONE");
ima.setVisibility(View.GONE);
}*/
break;
case DragEvent.ACTION_DRAG_ENDED :
System.out.println( "ACTION_DRAG_ENDED event");
break;
case DragEvent.ACTION_DROP:
Log.d(msg, "ACTION_DROP event");
break;
default: break;
}
return true;
}
});
the following code for touch listener
@Override
public boolean onTouchEvent(MotionEvent event) {
final int action = event.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
x_cord = (int) event.getX();
y_cord = (int) event.getY();
String text = "You click at x = " + event.getX() + " and y = " + event.getY();
//Toast.makeText(this, text, Toast.LENGTH_LONG).show();
System.out.println(text);
break;
}
case MotionEvent.ACTION_MOVE:{
x_cord = (int) event.getX();
y_cord = (int) event.getY();
String text = "You click at x = " + event.getX() + " and y = " + event.getY();
// Toast.makeText(this, text, Toast.LENGTH_LONG).show();
System.out.println(text);
break;
}
}
return true;
}
Upvotes: 5
Views: 10959
Reputation: 12300
Putting the (same) draglistener also on a view that fills your whole screen, doesn't that call ACTION_DRAG_LOCATION everywhere? (and you won't need the touch listener in that case)
Upvotes: 2