How can you tell if a View is visible on screen in Android? How can you tell if a View is visible on screen in Android? android android

How can you tell if a View is visible on screen in Android?


This code works for me:

public static boolean isVisible(final View view) {    if (view == null) {        return false;    }    if (!view.isShown()) {        return false;    }    final Rect actualPosition = new Rect();    view.getGlobalVisibleRect(actualPosition);    final Rect screen = new Rect(0, 0, getScreenWidth(), getScreenHeight());    return actualPosition.intersect(screen);}


int[] location = new int[2];view.getLocationOnScreen(location);

or

Rect rect = new Rect();view.getGlobalVisibleRect(rect);

Now use this location or rectangle to check if it is in your visible bounds or not. If it is simply the entire screen, check against getResources().getDisplayMetrics().

As pointed by Antek in the comments below, the view may still be gone or invisible with the returned values here telling where it was last drawn. So combining the above bounds-related condition with an view.isShown() or view.getVisibility() == VISIBLE should take care of that.


zegee29's answer is quite helping. Although I'd like to suggest using the result of view.getGlobalVisibleRect(actualPosition) too, because in some cases Rect.intersects() returns true when item is not visible at all, so the resulting code is:

fun View.isVisible(): Boolean {    if (!isShown) {        return false    }    val actualPosition = Rect()    val isGlobalVisible = getGlobalVisibleRect(actualPosition)    val screenWidth = Resources.getSystem().displayMetrics.widthPixels    val screenHeight = Resources.getSystem().displayMetrics.heightPixels    val screen = Rect(0, 0, screenWidth, screenHeight)    return isGlobalVisible && Rect.intersects(actualPosition, screen)}

Or you may just the result of getGlobalVisibleRect(actualPosition)