How can I check if a view is visible or not in Android?
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
}
How can I check if a View is visible (android listview)
You can use
getVisibility () Returns the visibility status for this view .
isShown () Returns the visibility of this view and all of its ancestors .
getVisibility()
if (ViewObj.getVisibility() == View.VISIBLE) {
// Your Staff
} else {
// Your Staff
}
isShown ()
True if this view and all of its ancestors are VISIBLE
Android: how to check if a View inside of ScrollView is visible?
Use View#getHitRect
instead of View#getDrawingRect
on the view you're testing. You can use View#getDrawingRect
on the ScrollView
instead of calculating explicitly.
Code from View#getDrawingRect
:
public void getDrawingRect(Rect outRect) {
outRect.left = mScrollX;
outRect.top = mScrollY;
outRect.right = mScrollX + (mRight - mLeft);
outRect.bottom = mScrollY + (mBottom - mTop);
}
Code from View#getHitRect
:
public void getHitRect(Rect outRect) {
outRect.set(mLeft, mTop, mRight, mBottom);
}
Related Topics
How to Set a Particular Font for a Button Text in Android
Handling a Menu Item Click Event - Android
Device Not Detected in Eclipse When Connected with Usb Cable
Visual Studio (2015) Emulator for Android Not Working - Xde.Exe - Exit Code 3
How to Disable Gridview Scrolling in Android
How to Share Same Data Between Multiple Activities
Ripple Effect on Android Lollipop Cardview
Display Badge on Top of Bottom Navigation Bar's Icon
Set Dynamic Base Url Using Retrofit 2.0 and Dagger 2
How to Make My Layout Scroll Both Horizontally and Vertically
How to Build Signed APK from Android Studio for Flutter
Android: How to Remove Margin/Padding in Preference Screen
Set Edittext Digits Programmatically
Getlastknownlocation Always Return Null After I Re-Install the APK File via Eclipse
How to Run Code on a Background Thread on Android