Android - negative x and y in onTouchEvent

    Android's handling of touch event recommends to use getX() and getY() when trying to determine the coordinates with respect to the actual view that is being touched. This makes sense for most use cases and developers might expect positive values starting from 0,0 to width-1,height-1.

   However, there are cases when getX() and getY() would return negative values and values greater than the actual view dimensions. The reason is due to padding added to the view. Padding unlike margin is internal to the view. Clicking on the padded space before the start of the view dimensions would result in a negative values. The best solution is just to check for the touch coordinates to be within the view boundary and then handle appropriately.

No comments: