Scrolling to HTML content in WebView

Is there a way to programmatically scroll into the HTML element that is displayed in the WebView ?

The WebView API only provides a method scrollTo(x,y)

, but I cannot find a way to determine the position of the displayed item.

Decision

Using Javascript:

webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("javascript:document.getElementById('id').scrollIntoView();");

      

+3


source to share


1 answer


Perhaps not directly with WebView, but with javascript. If you have a js function to scroll to an element like:

function scrollTo(element){
   document.getElementById(element).scrollIntoView();
}

      

You can call it from WebView with



mWebView.loadUrl("javascript:scrollTo('element')");

      

Just make sure javascript is enabled in the WebView

mWebView.getSettings().setJavaScriptEnabled(true);

      

+4


source







All Articles