Data is Scrollable or not in WebView Android?

Kamaldeep Kumar
2 min readDec 23, 2021

--

So, Many times we have used cases to find the data is scrollable or not in WebView depend on that, perform some action.

Photo by Markus Winkler on Unsplash

In My case, I have enabled the button when the user ends the page. For this, I have used the onOverScrolled() function.

override fun onOverScrolled(
scrollX: Int,
scrollY: Int,
clampedX: Boolean,
clampedY: Boolean
) {
super.onOverScrolled(scrollX, scrollY, clampedX, clampedY)
if (clampedY && scrollY > 0) {
// scrollable end.... logic here
}
}

It's working fine if we have scrollable content.

But what happens if data is not scrollable or may be open on a tablet in that case data is not scrollable and this override function is not called and our logic is not called. So In my case button is not enabled that is my use case.

So the Expected use cases are ……

  1. When data is not scrollable button should enable and
  2. When data is scrollable then the user goes to the end of the page and the button should enable it.

In this, my first use case is not working …I have found some workaround for this to find if data is not scrollable to make the button enabled …

override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
if (computeVerticalScrollRange() <= computeVerticalScrollExtent()) {
// logic here.....
}
}
  1. onPageFinished(): as the name suggests it's called when the whole page render is finished. So I put the condition in this function.
  2. computeVerticalScrollRage(): The scroll range of a scroll view is the overall height.
  3. computeVerticalScrollExtent(): This computes the current height of the screen.

So based on this condition, I can enable the button.

Thanks for reading, and I’ll come again with new learnings soon

--

--