Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to check if a View within a ScrollView is currently visible in Android. I am not checking if it is focused on yet but if it is currently being displayed on screen. Is there a method in View that can tell me if the view is currently visible?

like image 733
user1847544 Avatar asked Dec 26 '12 10:12

user1847544


People also ask

How do you make a view visible and invisible on Android?

use setVisibility method. for hiding button you can use button. setVisibility(View. GONE);.. you can use View.

How do I ask the display is visible or not?

"Do you see my screen" or "can you see my screen"? Either is correct.

How do I toggle visibility on Android?

You will need to set an OnClickListener() on the layout you want to toggle. Then inside the listener you check to see if the layout is visible or not. If it is, then you set its visibility to View. INVISIBLE .

What is Android visibility?

A View's visibility status is of Integer type and can have one of three options: VISIBLE (0) - The View is visible to the user. INVISIBLE (4) - The View is invisible to the user, but still takes up space in the layout. GONE (8) - The View is invisible, and it does not take up space in the layout.


3 Answers

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.

like image 23
AA_PV Avatar answered Oct 20 '22 05:10

AA_PV


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);
}
like image 114
zegee29 Avatar answered Oct 20 '22 04:10

zegee29


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)

like image 25
Диана Ганеева Avatar answered Oct 20 '22 05:10

Диана Ганеева