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?

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.

  • 1
    this does not work when toggling visibility - if your view was initially visible, then it will retain its visible rect dimensions. – Antek Jul 18 '17 at 11:04
  • You can sure add a check for view's visibility as well – AA_PV Jul 18 '17 at 13:24
  • what do you mean? The view has now visibility = GONE (I just toggled it, it had visibility = VISIBLE), yet the visible rectangle remains as it was when the view was created. No need to check it, as I set it myself in one place in code. – Antek Jul 18 '17 at 13:44
  • 1
    I meant depending on the intention, you can add the check for view.getVisibility() == VISIBLE along with the position returned on screen. For the question actually it would make sense to add it, I'll edit the answer. Thanks – AA_PV Jul 18 '17 at 15:39
  • @Antek It would be interesting to test this out if you're working around this - what happens when the view is drawn, then made GONE then never drawn again? I imagined GONE would update the location but if it doesn't it worth checking if we stay with a stale location going forward. Thanks! – AA_PV Jul 18 '17 at 15:46

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);
}

try

if(view.isShown()) {
    // Visible
} else { 
    // Invisible
}
  • 8
    This will be true if, for example, there is an overlay occluding the View so not reliable, nor the correct answer – r3flss ExlUtr Dec 5 '16 at 12:18
  • 1
    This is incorrect. isShown() returns the visibility property of the View. – The Hungry Androider Jul 13 '17 at 16:29
  • 1
    Why does this have several upvotes? It's wrong people! – hasen Jul 15 '17 at 2:57

The getVisibility() method will help you to check the visibility of any View.

Example:

if(view.getVisibility() == View.VISIBLE) {
    // Visible
} else { 
    // Invisible
}

The function View.getVisibility() can have below values:

  • View.VISIBLE (0): the view is visible.

  • View.INVISIBLE (1): The view is invisible, but it still takes up space for layout purposes.

  • View.GONE (2): the view is gone. Completely hidden, as if the view had not been added

You can see below link for more info. How can I check if a view is visible or not in Android?

  • 8
    Why not give credit to the original answer? stackoverflow.com/a/3791698/562935 – tidbeck Jun 25 '13 at 8:57
  • 4
    @tidbeck this answer is completely wrong! he wants to know view is visibile right now in screen or not! visibility attribute always return visible if you don't change it. this attribute is not related to scroll,it only modify by programmer. – Siavash Abdoli Jan 26 '16 at 7:23
  • By the way, constants for visible, invisible and gone are 0,4 and 8 respectively. – GAMA Mar 4 '16 at 13:13
  • 2
    I agree this answer is completely wrong. – J. K. Jun 2 '16 at 15:09

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.