4

I keep getting top, bottom, left, right = 0. I guess I am doing this wrong, what is the correct way? TIA

in onCreate()

    ImageView trash = (ImageView) findViewById(R.id.dropTarget_trash);
    trash.setOnTouchListener(this);
    Rect trashHit = new Rect();
    trash.getHitRect(trashHit);
    Log.d(TAG,"Trash left:" + trashHit.left + " right: " + trashHit.right + "  top: " + trashHit.top + " bottom: " + trashHit.bottom);
Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85
Jim
  • 1,966
  • 3
  • 24
  • 33

1 Answers1

4

As the hit rect is in the parent coordinate space, the parent first needs to layout its children which it hasn't done yet during onCreate(). Take a look at the solution here for an example of running in post(). If you have a custom view, getHitRect() within onDraw() will give you the correct dimensions too.

Community
  • 1
  • 1
dchang
  • 2,007
  • 14
  • 14