MotionEvent GetY() and getX() return incorrect values

glodos picture glodos · Jun 4, 2011 · Viewed 20.6k times · Source

I have following situation:

I have a custom ListView with ImageView and TextView in a row. The ImageView has an onTouchListener, wchich invokes my onTouch method. Here are some lines from it:

if (event.getAction()==MotionEvent.ACTION_MOVE) {
    layout.leftMargin = (int) event.getX() - dragIcon.getWidth()/2;         
    layout.topMargin = (int) event.getY() - dragIcon.getHeight()/2;
    //Log.d("Tag", "Pozycja: " +  event.getX() +", "+  event.getY());
}
dragIcon.setLayoutParams(layout);

When move is detected I'm showing up new image (not this in ListView) and I'm starting to move it according to x and y coordinates.

The problem is, that getX and getY return positions relative to ImageView in the list, not the whole ListView (I think so). So when I touch an item in the middle and swipe finger up, then getY returns negative values (above ImageView boundary).

Hope, I explained clearly..

Any ideas how to get this coordinates relative to the screen size? Thank you.

Answer

Kevin Coppock picture Kevin Coppock · Jun 4, 2011

Try using getRawX() and getRawY() instead of getX() and getY().