Jeffrey Blattman Jeffrey Blattman - 1 year ago 112
Android Question

android get bounding rectangle of a view

i'm implementing a drag and drop for an android application. in order to know if the drop happens inside the drop target, i need to know the bounding rectangle of the drop target view. i would then see if the getRawX/Y() in the MotionEvent fall within this rect when i get the ACTION_UP action.

i realize i can call getLeft/Right/Top/Bottom() on the drop target view, but these are relative to the parent's container. it seems i need to know the "real" or raw values so i can compare them to the raw x, y in the MotionEvent.

Answer Source

Answering my own question ... yes, View.getLocationOnScreen() did the trick. For example,

private boolean isViewContains(View view, int rx, int ry) {
    int[] l = new int[2];
    int x = l[0];
    int y = l[1];
    int w = view.getWidth();
    int h = view.getHeight();

    if (rx < x || rx > x + w || ry < y || ry > y + h) {
        return false;
    return true;