iiinatt iiinatt - 8 months ago 48
Android Question

How to find element in View by coordinates x,y Android

If i know coordinates(X,Y) pixel (by OnTouchEvent method and getX(),getY) how i can find element ex. button or text etc.... by use X,Y


You could use getHitRect(outRect) of each child View and check if the point is in the resulting Rectangle. Here is a quick sample.

for(int _numChildren = getChildCount(); --_numChildren)
    View _child = getChildAt(_numChildren);
    Rect _bounds = new Rect();
    if (_bounds.contains(x, y)
        // In View = true!!!

Hope this helps,