I’m adding ImageViews to parent layout dynamically and performing zoom in/out operations with onTouch on the added image.
I want to remove the added view with an onLongPress of it.
img.setOnLongClickListener(longClickAction);
img.setOnTouchListener(touchAction);
onLongPress:
OnLongClickListener longClickAction = new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
parentLayout.removeView((ImageView)v);
return false;
}
};
onTouch:
OnTouchListener touchAction = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
ImageView i = (ImageView)v;
// Perform zoom operation onTouch of ImageView
zoom(i, event);
return true;
}
};
Why do only the onTouch events work?
How can I get them both to work?
What should I do to remove the added view?
onTouchis always called for your view since this is the initial state of dispatching the events to the view. When you long press your view this still callsonTouchfirst and since you returntrueinonTouch(which means that you’ve consumed this event and it should not be further dispatched) you won’t getonLongPresscalled. What will do the trick is returningfalseinonTouch