0
    @Override public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //movement
                initialX = params.x;
                initialY = params.y;
                initialTouchX = event.getRawX();
                initialTouchY = event.getRawY();
                return false;

            case MotionEvent.ACTION_UP:
                return false;

            case MotionEvent.ACTION_MOVE:
                //movement
                params.x = initialX + (int) (event.getRawX() - initialTouchX);
                params.y = initialY + (int) (event.getRawY() - initialTouchY);

                SharedPreferences.Editor pos = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
                pos.putInt("x",params.x);
                pos.putInt("y",params.y);
                pos.commit();

                windowManager.updateViewLayout(chatHead, params);
                return true;
        }
        return false;
    }
});

chatHead.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(getApplicationContext(), "Short Press", Toast.LENGTH_SHORT).show();
    }
});

chatHead.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        Toast.makeText(getApplicationContext(), "Long Press", Toast.LENGTH_SHORT).show();
        return true;
    }
});

Here's my onTouch method. I setted onTouchListener, onClickListener, and onLongClickListener to a single image view. The code seems to works fine, but when I move the image only for a short time, short click or long click runs also. How do I fix this problem?

Pang
  • 9,564
  • 146
  • 81
  • 122
Choi
  • 1

1 Answers1

0

First of all for that you only have to use onTouchListener and with the help of some conditions you can detect onClick and onLongClick

  • For the onClick put condition on MotionEvent.ACTION_UP

    if(initialTouchX == event.getRawX() && initialTouchY == event.getRawY()){
         //on Click Detected
    }
    
  • For the OnLongPress try this One

    setOnTouchListener(new View.OnTouchListener() {
         public boolean onTouch(View v, MotionEvent event) {
    
            int action = MotionEventCompat.getActionMasked(event);
    
    
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    longClick = false;
                    x1 = event.getX();
                    break;
    
                case MotionEvent.ACTION_MOVE:
                    if (event.getEventTime() - event.getDownTime() > 500 && Math.abs(event.getX() - x1) < MIN_DISTANCE) {
                        longClick = true;
                    }
                    break;
    
                case MotionEvent.ACTION_UP:
                            if (longClick) {
                                Toast.makeText(activity, "Long preess", Toast.LENGTH_SHORT).show();
                            } 
            }
            return true;
        }
    });
    
Keyur Lakhani
  • 4,321
  • 1
  • 24
  • 35