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
Determine if a view is on screen - Android
Ok so thanks to OceanLife for pointing me in the right direction! There was indeed a callback required and ViewTreeObserver.OnGlobalLayoutListener()
did the trick. I ended up implementing the listener against my fragment class and picked it up where I needed it. Thanks for the warning too regarding the multiple calls, I resolved this using the removeOnGlobalLayoutListener()
method - works a charm.
Code:
...
// vto initialised in my onCreateView() method
vto = getView().getViewTreeObserver();
vto.addOnGlobalLayoutListener(this);
...
@Override
public void onGlobalLayout() {
final int i[] = new int[2];
final Rect scrollBounds = new Rect();
sView.getHitRect(scrollBounds);
tempView.getLocationOnScreen(i);
if (i[1] >= scrollBounds.bottom) {
sView.post(new Runnable() {
@Override
public void run() {
sView.smoothScrollTo(0, sView.getScrollY() + (i[1] - scrollBounds.bottom));
}
});
}
vto.removeOnGlobalLayoutListener(this);
}
Just got to do some cleaning up now ...
Related Topics
Notification Disappears After Showing
How to Close the Current Fragment by Using Button Like the Back Button
How to Get Context in Android Mvvm Viewmodel
How to Open Draw Overlay Permission Popup in Miui
How to Keep a Bottom Nav Bar from Being Pushed Up on Keyboard Shown
How to Set Transparency of a Background Image Android Xml File
How to Set the Height of an Item Row in Gridlayoutmanager
Android: Textview: Remove Spacing and Padding on Top and Bottom
Android How to Get Current Positon on Recyclerview That User Scrolled to Item
Android Volley - Basicnetwork.Performrequest: Unexpected Response Code 400
How to Start an Activity from Background in Android 10
Suppress Keyboard from Popping Up
Changing Cardview Shadow Color
Get City Name and Postal Code from Google Place API on Android
How to Verify Purchase for Android App in Server Side (Google Play in App Billing V3)
Error Androidmanifest.Xml Doesn't Exist or Has Incorrect Root Tag