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.


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;

You can also use a Rect here:

private boolean isViewContains(...) {
    int[] l = new int[2];
    Rect rect = new Rect(l[0], l[1], l[0] + imageView.getWidth(), l[1] + imageView.getHeight());
    return rect.contains(rx, ry);

Less wordy, and possibly faster, but certainly (IMO) more readable.

This code takes into consideration, the perimeter of the Views involved and only returns true when the dragged View is fully inside the drop zone.

public boolean containsView(View dropZone, View draggedView){
     // Create the Rect for the view where items will be dropped
     int[] pointA = new int[2];
     Rect rectA = new Rect(pointA[0], pointA[1], pointA[0] + dropZone.getWidth(), pointA[1] + dropZone.getHeight());

     // Create the Rect for the view been dragged
     int[] pointB = new int[2];
     Rect rectB = new Rect(pointB[0], pointB[1], pointB[0] + draggedView.getWidth(), pointB[1] + draggedView.getHeight());

     // Check if the dropzone currently contains the dragged view
     return rectA.contains(rectB);

Need Your Help

Copying an array of objects into another array in javascript (Deep Copy)

javascript arrays node.js google-chrome deep-copy

Copying an array of objects into another array in javascript using slice(0) and concat() doesnt work.