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?
This code works for me:
public static boolean isVisible(final View view) {
if (view == null || !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);
}
Resources.getSystem().getDisplayMetrics().widthPixels
and Resources.getSystem().getDisplayMetrics().heightPixels
–
Plaided 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.
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. –
Lyn 0,0
either way (fullscreen view group) –
Lyn location
they meant upper-left corner of a view (which can have some dimensions) - so any view located there, regardless of size, will have such location. –
Lyn boundingBox
? –
Lyn zegee29's answer is quite helping. Although I'd like to suggest using the result of view.getGlobalVisibleRect(actualPosition)
too, because in some cases Rect.intersects()
returns true
when item is not visible at all, so the resulting code is:
fun View.isVisible(): Boolean {
if (!isShown) {
return false
}
val actualPosition = Rect()
val isGlobalVisible = getGlobalVisibleRect(actualPosition)
val screenWidth = Resources.getSystem().displayMetrics.widthPixels
val screenHeight = Resources.getSystem().displayMetrics.heightPixels
val screen = Rect(0, 0, screenWidth, screenHeight)
return isGlobalVisible && Rect.intersects(actualPosition, screen)
}
Or you may just the result of getGlobalVisibleRect(actualPosition)
Java variant of Диана Ганиева's post:
boolean isViewOnScreen(View target)
{
if (!target.isShown())
{
return false;
}
final var actualPosition = new Rect();
final var isGlobalVisible = target.getGlobalVisibleRect(actualPosition);
final var screenWidth = Resources.getSystem().getDisplayMetrics().widthPixels;
final var screenHeight = Resources.getSystem().getDisplayMetrics().heightPixels;
final var screen = new Rect(0, 0, screenWidth, screenHeight);
return isGlobalVisible && Rect.intersects(actualPosition, screen);
}
try
if(view.isShown()) {
// Visible
} else {
// Invisible
}
View
so not reliable, nor the correct answer –
Lashaun public boolean checkVisiblity(View view) {
return view.isShown();
}
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?
© 2022 - 2024 — McMap. All rights reserved.