Search code examples
androidgesturedetectorgesturelistener

longPress does not reply to ACTION_UP - simple gesture listener


i m trying to make imageView increase in size on longpress and come back to normal after i unpress it.

public class MainActivity extends Activity {
private class Erjan_gestures extends SimpleOnGestureListener{
    @Override
    public void onLongPress(MotionEvent event) {
        Log.wtf("x", "long press occurring");

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.wtf("x", "LONG PRESS - action down");
                image.getLayoutParams().height = 400;
                image.getLayoutParams().width = 400;
                RelativeLayout.LayoutParams for_answer1 = new   RelativeLayout.LayoutParams(300, 600);
                image.requestLayout();
                break ;
            case MotionEvent.ACTION_UP:
                //THIS CASE IS NEVER REACHED
                Log.wtf("x", "LONG PRESS - action up");
                image.getLayoutParams().height = oldH;
                image.getLayoutParams().width = oldW;
                image.requestLayout();
                break;
        }
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    image= (ImageView)findViewById(R.id.card);
    button=(Button)findViewById(R.id.button);

    oldW = 500;
    oldH = 600;

    gestureDetector = new GestureDetector(new Erjan_gestures());
    gestureDetector.setIsLongpressEnabled(true);
    image.setOnTouchListener(new View.OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_UP){
                Log.wtf("x", "action up is detected");
            }
            Log.wtf("x", "I m a card, and i know you click on me!");
            if(gestureDetector.onTouchEvent(event)) {
                Log.wtf("x", "this is onTouch(View v, MotionEvent event)");
                return true;
            }
            else return false;
        }
    });
}

However my imageView does detect longpress and does execute ACTION_DOWN, but never gets to ACTION_UP part in longpress().

Is this because longpress is not supposed to be divided into action_up,down?

  1. Long press gesture itself only consists of press(aka ACTION_DOWN)?
  2. why action_up in longPress never gets executed?

Solution

  • This is indeed because longpress is not divided into down and up, but rather only has a "trigger" action.

    Actually, ACTION_DOWN is even an incorrect term. longpress doesn't have anything to do with ACTION_DOWN, as the ACTION_DOWN does not get triggered right when the user presses the button. It only gets triggered after a specific delay of holding down. DELAY_PASSED or so would therefore be a more suitable name.

    Do note that the normal press still continues, and that its ACTION_UP will still fire.