Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
like image 939
ERJAN Avatar asked Oct 13 '25 10:10

ERJAN


2 Answers

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.

like image 57
Daniël van den Berg Avatar answered Oct 15 '25 01:10

Daniël van den Berg


Here's a solution provided by using the Holder class and the onTouch() method.

public class MainActivity extends AppCompatActivity {
    boolean is_pressed = false;    
    ImageView image;
    Button button;
    int oldW, oldH;

    private final Handler handler = new Handler();
    private final Runnable runnable = new Runnable() {
        public void run() {
            if (is_pressed) {
                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();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);    
        image = (ImageView) findViewById(R.id.imageView);
        button = (Button) findViewById(R.id.button);    
        oldW = 500;
        oldH = 600;

        image.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
            Log.wtf("x", "touching");
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                is_pressed = true;
                // 500ms - to determine that this is a long press
                handler.postDelayed(runnable, 500);
            } else if (event.getAction() == MotionEvent.ACTION_UP && is_pressed) {                 
                Log.wtf("x", "LONG PRESS - action up");
                image.getLayoutParams().height = oldH;
                image.getLayoutParams().width = oldW;
                image.requestLayout();
                is_pressed = false;
            } else return false;
            return true;
        }
        });
    }
}
like image 21
Movsar Bekaev Avatar answered Oct 15 '25 00:10

Movsar Bekaev