I have a RelativeLayout with two views inside. The view1 is recreated inside the layout in a random position every ten seconds. view2 is in a static position and is bigger then view1. I want to know when the first view is created inside the second view area, how can I do that?
I'm currently trying this code but id doesn't work well.
if (paramsView1.topMargin > View2Ystart
&& paramsView1.topMargin < View2Yend
&& paramsView1.leftMargin > View2Xstart
&& paramsView1.leftMargin < View2Xend) {
return true
}
else
return false;
It returns true only if view1 is touching a side of view2. I want it returns true only if view1 is totally inside view2.
You should use
getLeft()
,getRight()
,getTop()
andgetBottom()
.Be mindful that these values will be available when the parent is laid out, i.e. not immediately after
addView()
.Another possible solution, which may be more flexible, is to build
Rect
instances with each view's coordinates, e.g.Then you can use
rect1.contains(rect2)
orRect.intersects(rect1, rect2)
or any other combination.