How can I check if a view is visible or not in Android? [duplicate]
Asked Answered
M

4

215

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?

Mvd answered 24/9, 2010 at 23:1 Comment(0)
B
527

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
}
Berey answered 24/9, 2010 at 23:20 Comment(3)
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 answer is working all the time.Pantograph
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.Bellyband
@BillMote My solution was to solve the original question. How to get the value of the visibility attribute.Berey
A
141

Or you could simply use

View.isShown()
Analyzer answered 23/1, 2012 at 23:2 Comment(4)
This is more accurate as it checks if the view is on the displaylist as a view could be "visible" but have no parent.Wolffish
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 answer is working all the time.Pantograph
View#isShown() traverses all the parent view's visibility to verify that it's shown on screen. This procedure can be costly sometimes.Nucleoside
I think this should be accepted answer.Terrarium
B
72

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
}
Bellyband answered 14/9, 2012 at 16:8 Comment(6)
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.Suction
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.Bellyband
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; }Suction
It doesn't consider ScrollView padding!Dynameter
this (getLocalVisibleRect) looks promising, but can we just using getGlobalVisibleRect() instead? If not, why? Thanks.Leverett
scrollView.getHitRect(scrollBounds) is not doing anything, the rect is overwritten when getLocalVisibleRect calls getGlobalVisibleRectLusatian
D
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.

Deherrera answered 24/9, 2010 at 23:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.