2

I'm trying to calculate the rectangle which wraps a group of elements. For example I have a couple of elements with different widths and I have to calculate the rectangle which is wrapping them. Could you help me with this calculation ?

Example Image of how is the layout

StoyanM
  • 33
  • 1
  • 5
  • Please let us know what you've tried and how it compared to what you want. Then we can help direct you toward a solution. – Steven Hood May 07 '14 at 06:24

1 Answers1

2

You can calculate it:

Take the minimum of x and y coordinates of every element. That will be the top left corner of the Rectangle.

Then take the maximum of x+width, and y+height of every element. That will be the bottom right corner of the Rectangle.

Or you can let Android calculate it:

Wrap those elements with a FrameLayout. Set both dimensions of the frame to wrap_content. This way the frame will be the rectangle that you are looking for. (This solution assumes that there are no margin on the wrapped items.)

kupsef
  • 3,357
  • 1
  • 21
  • 31
  • Hi kupsef, thanks for you answer. The solution with wrapping all elements in a layout is not usable in my case - this would be the easiest solutions, just to wrap them :) I should take the maximum x and width of every element, right ? In fact now I'm facing some problems with calculation of the X & Y and width and height of a TextView. If element is TextView - Y always in 0 no matter where is it position. And because I want to check if I clicked on this rectangle, the second click the width of the TextView is different. I think that is because the width/height is "wrap_content" – StoyanM May 07 '14 at 07:14
  • You should take the maximum of (x+width) and (y+height) to determine the bottom right corner. If you want to measure your views, you have to let android measue it first. See this post: [Link](http://stackoverflow.com/questions/3602026/linearlayout-height-in-oncreate-is-0) – kupsef May 07 '14 at 08:06
  • Thanks, kupsef. The problem with the TextView was because I was using an empty TextView in the layout without to notice. – StoyanM May 07 '14 at 08:17