Android Question How to scroll a webview programmatically

thedesolatesoul

Expert
Licensed User
Longtime User
Okay, this is a challenging one.

I want to create a (fast) scrollbar for a webview.
The problem is to get the webview content height.
I tried using WebViewExtras to get the Content Height
and the WebViewExtender to use ScrollTo functions.
However they dont seem to match and I suspect the Content Height is not what I need but maybe I need computeVerticalScrollRange which is a protected method and requires to subclass WebView?

I have tried various combinations of the following code (with a webview and a seekbar).

Code: Copy
Sub wv_PageFinished (Url As String)
    pageHeight = ( WVExtras.GetContentHeight(WV) * WVExtras.GetScale(WV) )
    Log("PageHeight: " & pageHeight)
End Sub

Sub sb_ValueChanged (Value As Int, UserChanged As Boolean)
    Log(Value & " " & pageHeight & " " & WVExtnder.getScrollY(WV) & " " & WVExtnder.getScale(WV) )
    WVExtnder.ScrollTo(WV,0, ( Value*(pageHeight/100) *(100dip/100) ))
End Sub
 
Thanks to kind @warwound for his solution.

What I was doing was fairly correct except that getContentHeight does not finalize in PageFinished. It seems it settles long after, so maybe PageFinished only triggers when the page is downloaded?

Anyway, this code works:
Code: Copy
Sub sb_ValueChanged (Value AsInt, UserChanged AsBoolean)
 WVExtnder.ScrollTo(WV,0, ( Value*(( WVExtras.GetContentHeight(WV) * WVExtras.GetScale(WV) )/100) ))

End Sub
where sb.Max = 100
 
Upvote 0
Back
Top