Android: How to determine if a view is onscreen, offscreen or partially offScreen/partially visible?

I have come across this question a plenty number of time. The solution is quiet simple:

For a given view v, one can determine the Global Visible Rectangle of the view v by:

In the above code snippet globalVisibilityRectangle variable will be populated with coordinates of the view which are only visible on screen. From the globalVisibilityRectangle variable we can extract visible height and visible width of the view, like this:

Now is the part where we want to grab the actual height and actual width of the view v:

If the view is onScreen(not even overshadowed by any other view) then the below statement will be true:

If the view is partiallyVisible (partially over shadowed by another view or partially off screen) then the below statement will be true:

If the view is totally offScreen(not even a small part visible) then the below statement will be true:

I hope, it helps.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store