Ari
Ari

Reputation: 1470

Scroll to HTML content in WebView

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

The WebView API only provides a scrollTo(x,y) method, but I can't find a way to determine the position of a displayed element.

Solution

Using Javascript:

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

Upvotes: 4

Views: 4063

Answers (1)

inmyth
inmyth

Reputation: 9070

Maybe not directly with WebView but with javascript. If you have a js function to scroll to an element such as this :

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 WebView

mWebView.getSettings().setJavaScriptEnabled(true);

Upvotes: 5

Related Questions