0

I want to get the X & Y coordinates of the location where i long click and set the button to this location, but i don't get it because there is no MotionEvent as with the onClick method.

    private View.OnLongClickListener layoutOnTouchListener(){
    return new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            RelativeLayout.LayoutParams positionRules = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            positionRules.leftMargin = (int) v.getX();
            positionRules.topMargin = (int) v.getY();
            mainButton.setLayoutParams(positionRules);
            Log.d("X", String.valueOf(v.getX()));
            return true;
        }
    };
}

Thats the code i tried.

Oliver Weidner
  • 305
  • 1
  • 3
  • 11
  • [here is a post](https://stackoverflow.com/questions/6530350/android-press-longclicklistener-get-x-y-coordinates-ontouchlistener) that should solve your exact problem, solved by Suragch – Totalattak Dec 06 '18 at 14:47

1 Answers1

0

You can try setonkeyListener and check whether it's long click or not

@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
    if (null != keyEvent && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {

        if (System.currentTimeMillis() - mLastClickedTime > DEFAULT_PRESSED_DELAY) {
            Log.d(TAG, "onKey: single  presed");
            onKeyPressed(view, keyEvent);

        } else {
            Log.d(TAG, "onKey: repeat pressed");
            onKeyRepeatPressed(view);

        }
        mLastClickedTime=System.currentTimeMillis();

    }
    return false;
}

If it's long click take the x and y points