Android: how to check if a View inside of ScrollView is visible?
This works:
Rect scrollBounds = new Rect();scrollView.getHitRect(scrollBounds);if (imageView.getLocalVisibleRect(scrollBounds)) { // Any portion of the imageView, even a single pixel, is within the visible window} else { // NONE of the imageView is within the visible window}
Use View#getHitRect
instead of View#getDrawingRect
on the view you're testing. You can use View#getDrawingRect
on the ScrollView
instead of calculating explicitly.
Code from View#getDrawingRect
:
public void getDrawingRect(Rect outRect) { outRect.left = mScrollX; outRect.top = mScrollY; outRect.right = mScrollX + (mRight - mLeft); outRect.bottom = mScrollY + (mBottom - mTop); }
Code from View#getHitRect
:
public void getHitRect(Rect outRect) { outRect.set(mLeft, mTop, mRight, mBottom);}
If you want to detect that the view is FULLY visible:
private boolean isViewVisible(View view) { Rect scrollBounds = new Rect(); mScrollView.getDrawingRect(scrollBounds); float top = view.getY(); float bottom = top + view.getHeight(); if (scrollBounds.top < top && scrollBounds.bottom > bottom) { return true; } else { return false; }}