iiinatt iiinatt - 3 months ago 19
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

Answer

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();
    _child.getHitRect(_bounds);
    if (_bounds.contains(x, y)
        // In View = true!!!
}

Hope this helps,

FuzzicalLogic