Uma Sankar
Uma Sankar

Reputation: 479

Check If a view is completely hidden by other views (not window)

I have two views A and B. View B is completely covered view A which means the bounding rect of view B is more than bounding rect of view A.

The best example I can give is, I have a fixed bottom tabs layout and a RecyclerView with multiple linear layouts. When I scroll, one of the linear layout will be behind the bottom tabs layout.

So, How can I programatically check If view A is completely covered by other views (other views might not be known upfront)?

Is there any possible way to identify this?

Update: I have tried the solution mentioned here. But it didn't solve my problem. In my case, the view bounds are exactly matching and I want which view is on top.

Upvotes: 0

Views: 1185

Answers (2)

Uma Sankar
Uma Sankar

Reputation: 479

After lot of struggle, I have found a way to identify if view is overlapped by other views in the following way.

AccessibilityNodeInfo nodeInfo = AccessibilityNodeInfo.obtain();
viewA.onInitializeAccessibilityNodeInfo(nodeInfo);

if (!nodeInfo.isVisibleToUser()) {
    // View is not visible to user. This also validates if viewA is overlapped by other views
}

nodeInfo.recycle();

Upvotes: 5

Alok Singh
Alok Singh

Reputation: 660

Use the following method it will help you to find view overlapping:-

private boolean isYourViewOverlapping(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);
    }

Upvotes: 0

Related Questions