2

I have a piece of code where I am displaying some pins (Button with an image) on an image (full screen ImageView). (You can assume that all the pins are created dynamically and have a random position). The requirement is that those pins should not overlap. For each pin created I am checking if it overlaps with any of the other previously created pins.

To do that I am trying to get the Rect of those pins and check if they overlap using Rect.intersects(). The issue is that I cannot use functions like getRight(), getTop(), getWidth(), getHeight() before the view is actually displayed. They all return 0. So I cannot get the correct data for the rectangles. I tried to put the code in different callbacks like onWindowFocusChanged() or onResume() but I still got the same issue, all dimensions/positions return 0.

I am looking for a solution/advice regarding this problem. How to get the correct positions and dimensions before the pins are displayed ?

I looked at View.measure(), it seems I can get some values using getMeasuredWidth() and getMeasuredHeight() but getRight() and getTop() still return 0.

jackslash
  • 8,550
  • 45
  • 56
Man0waR
  • 31
  • 1

1 Answers1

0

Add invisible rect first and check the position then move this position as per requirements and the make this visible. This will do the trick.

Pramod
  • 1,123
  • 2
  • 12
  • 33