4

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();"); 

1 Answer 1

5

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); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.