1

I wrote the following code to find out the coordinates of touch point.

view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                float val = view.getAlpha();
                float xVal = view.getX();
                float yVal = view.getY();
                String message = "Screen height: " + height + ", width: " + width + "\n" +
                        "Alpha Value: " + val + ", x Coordinate: " + xVal + ", y Coordinate: " + yVal;
                Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
                return false;
            }

It turns out to be always returning the fixed value, 32.0 for each x coordinate and y coordinate.

marshallslee
  • 645
  • 1
  • 8
  • 22

1 Answers1

2

Instead of getting the x and y coordinates via view.getX() and view.getY() respectively inside the onTouch() method, the following worked.

float xVal = motionEvent.getX(); float yVal = motionEvent.getY();

marshallslee
  • 645
  • 1
  • 8
  • 22