Question

I have a WebView with a number of anchors like this:

<a name='some_id'></a>

I am able to jump to the anchors using something like:

webView.loadUrl("javascript:(function() { " +
    "window.location.hash='#" + hash + "';" +
"})()");

But is it possible to smoothly scroll to the anchors somehow?

Was it helpful?

Solution

Unfortunately this would require a bit of effort on your end:

  1. Instead of performing a fragment navigation, you'd need to determine the position of the anchor element on the page. You'd need to use addJavaScriptInterface to convey this back to the Java side, don't forget to convert the CSS pixels you get from JS to physical pixels by multiplying by webview.getScale().

  2. Use an OverScroller to drive the animation to the desired scroll offset.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top