iiinatt iiinatt - 1 year ago 110
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 Source

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,


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download