smail2133 smail2133 - 3 months ago 20
Android Question

detect if views are overlapping

I have problem with drawing views on another size screens!
I need method which has two parameters of View type. And return true if first view overlapping on second view, and false in another case!

enter image description here

and

enter image description here

Answer

Berserk thanks you for help! After some experiments I wrote method which detect view is overlapped or not for my case!

private boolean isViewOverlapping(View firstView, View secondView) {
        int[] firstPosition = new int[2];
        int[] secondPosition = new int[2];

        firstView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        firstView.getLocationOnScreen(firstPosition);
        secondView.getLocationOnScreen(secondPosition);

        int r = firstView.getMeasuredWidth() + firstPosition[0];
        int l = secondPosition[0];
        return r >= l && (r != 0 && l != 0);
    }
Comments