214

I set visibility to invisible like this on Android:

myImageView.setVisibility(View.INVISIBLE);

And then to make it visible:

myImageView.setVisibility(View.VISIBLE);

Now I don't know if myImageView is visible or not, how can I check it like this:

if (myImageView IS VISIBLE) {
    Do something
} else {
    Do something else
}

How can I do that? What do I have to write within the brackets?

Idrizi.A
  • 9,819
  • 11
  • 47
  • 88
Martin
  • 2,319
  • 4
  • 17
  • 13

4 Answers4

526

Although View.getVisibility() does get the visibility, its not a simple true/false. A view can have its visibility set to one of three things.

View.VISIBLE The view is visible.

View.INVISIBLE The view is invisible, but any spacing it would normally take up will still be used. Its "invisible"

View.GONE The view is gone, you can't see it and it doesn't take up the "spot".

So to answer your question, you're looking for:

if (myImageView.getVisibility() == View.VISIBLE) {
    // Its visible
} else {
    // Either gone or invisible
}
William
  • 15,465
  • 8
  • 36
  • 32
  • 5
    Can be Visible while it is not within the confines of the visible screen, so that won't be accurate in all cases. However, [Bill Mote's](http://stackoverflow.com/a/12428208/62921) answer is working all the time. – ForceMagic Mar 06 '14 at 21:26
  • 1
    ForceMagic is correct and the reason I posted my answer. A View can be set to View.VISIBLE and your if-logic will return true, however, the view may not actually be visible to the user. My solution, below, will return true if, and only if, the user can actually see any portion of the View on the screen's view port. – Bill Mote Sep 29 '14 at 12:34
  • @BillMote My solution was to solve the original question. How to get the value of the visibility attribute. – William Oct 08 '14 at 19:43
140

Or you could simply use

View.isShown()
deviato
  • 2,027
  • 1
  • 15
  • 9
  • 7
    This is more accurate as it checks if the view is on the displaylist as a view could be "visible" but have no parent. – user123321 Apr 12 '13 at 23:54
  • 4
    Can be true while it is not within the confines of the visible screen, so that won't be accurate in all cases. However, [Bill Mote's](http://stackoverflow.com/a/12428208/62921) answer is working all the time. – ForceMagic Mar 06 '14 at 21:26
  • View#isShown() traverses all the parent view's visibility to verify that it's shown on screen. This procedure can be costly sometimes. – Ian Wong Aug 30 '17 at 00:02
  • I think this should be accepted answer. – Stanley Ko Nov 03 '20 at 13:06
70

If the image is part of the layout it might be "View.VISIBLE" but that doesn't mean it's within the confines of the visible screen. If that's what you're after; this will work:

Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
if (imageView.getLocalVisibleRect(scrollBounds)) {
    // imageView is within the visible window
} else {
    // imageView is not within the visible window
}
Bill Mote
  • 12,644
  • 7
  • 58
  • 82
  • Would you mind explaining why this works? I've looked at source code for View.getLocalVisibleRect and I am not sure of how it works. – yarian Sep 18 '12 at 20:32
  • 1
    There are 2 things going on here. 1) the View.getHitRect(scrollBounds) gets the hit rectangle in its parent's coordinates and populates them in scrollBounds and 2) We seed the ImageView's View.getLocalVisibleRect(scrollBounds) with scrollBounds. If the ImageView falls within the boundaries of scrollBounds the method returns true. – Bill Mote Sep 18 '12 at 21:57
  • 1
    Note: Having trouble formatting comment below. Apologies. Right, my bad for not specifying. The exact point of confusion is the `imageView.getLocalVisibleRect(scrollBounds)` call. From grepcode: `public final boolean getLocalVisibleRect(Rect r) { Point offset = new Point(); if (getGlobalVisibleRect(r, offset)) { r.offset(-offset.x, -offset.y); // make r local return true; } return false; }` – yarian Sep 19 '12 at 21:20
  • It doesn't consider ScrollView padding! – Ali Jun 18 '14 at 12:51
  • 1
    this (getLocalVisibleRect) looks promising, but can we just using getGlobalVisibleRect() instead? If not, why? Thanks. – Henry Aug 21 '20 at 16:25
  • `scrollView.getHitRect(scrollBounds)` is not doing anything, the rect is overwritten when `getLocalVisibleRect` calls `getGlobalVisibleRect` – hmac Oct 20 '22 at 17:57
3

You'd use the corresponding method getVisibility(). Method names prefixed with 'get' and 'set' are Java's convention for representing properties. Some language have actual language constructs for properties but Java isn't one of them. So when you see something labeled 'setX', you can be 99% certain there's a corresponding 'getX' that will tell you the value.

colithium
  • 10,269
  • 5
  • 42
  • 57