3

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,048
  • 6
  • 49
  • 84
Jim
  • 1,868
  • 3
  • 23
  • 33

1 Answers1

3

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
  • 1,997
  • 13
  • 14