I have imageView. Im using pan,pinch for imageView. Sometimes i need to delete imageView. So, i used OnLongClickListener for remove image. When i use long click my imageView removing. But when i use OnTouchListener for pan,pinch for imageView OnLongPress activated and my imageView removed from view. How to solve this?
code:
imageView.setOnLongClickListener(new OnLongClickListener(){
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
imageView.setVisibility(View.GONE);
return true;
}
});
imageView.setOnTouchListener(new View.OnTouchListener() {
final Handler handler = new Handler();
Runnable mLongPressed = new Runnable() {
public void run() {
Log.i("", "Long press!");
}
};
@Override
public boolean onTouch(View v,MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN)
handler.postDelayed(mLongPressed, 1000);
if((event.getAction() == MotionEvent.ACTION_MOVE)||(event.getAction() == MotionEvent.ACTION_UP))
handler.removeCallbacks(mLongPressed);
layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN :
{
parms = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
dx = event.getRawX() - parms.leftMargin;
dy = event.getRawY() - parms.topMargin;
}
break;
case MotionEvent.ACTION_MOVE :
{
x = event.getRawX();
y = event.getRawY();
parms.leftMargin = (int) (x-dx);
parms.topMargin = (int) (y - dy);
imageView.setLayoutParams(parms);
}
break;
case MotionEvent.ACTION_UP :
{
}
break;
}
return false;
}
});
}
});
You can use a handler to do this but you need to remember to cancel the handler if the user takes their finger off the screen. Yogesh is not totally wrong but the approach above simply adds a 1000 ms delay between onClick and when the runnable is executed. That means if the user lifts their finger the runnable will still run. This is not a true longpress.
Below you can see that I'm still using a handler with a 1000 ms delay (you can set it to whatever you want) but remove the callback if the user lifts their finger up or moves. If you want to get rid of the move trigger just delete that part of the call. But to affect long press you need to account for the lift to ensure the user holds their finger the whole time.
final Handler handler = new Handler();
Runnable mLongPressed = new Runnable() {
public void run() {
Log.i("", "Long press!");
}
};
@Override
public boolean onTouchEvent(MotionEvent event, View v){
if(event.getAction() == MotionEvent.ACTION_DOWN)
handler.postDelayed(mLongPressed, 1000);
if((event.getAction() == MotionEvent.ACTION_MOVE)||(event.getAction() == MotionEvent.ACTION_UP))
handler.removeCallbacks(mLongPressed);
return false;
}