ivesingh
ivesingh

Reputation: 888

AutoScrolling a WebView Android

I want to autoscroll my WebView, what's the best way to approach? I have tried the following but doesn't seem to be working.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final WebView wv = (WebView) findViewById(R.id.webView1);
    wv.loadUrl("www.google.com");
    wv.scrollBy(0,2);
  }
}

Upvotes: 0

Views: 850

Answers (1)

Sdr
Sdr

Reputation: 1092

Android (Java) based code failed to achieve the purpose, hence resorting to html/Javascript method (courtesy: http://www.mediacollege.com/internet/javascript/page/scroll.html).

Please add the following code to your html page between the HTML 'head' tags.

    <script>
    function pageScroll() {
        window.scrollBy(0,50); // horizontal and vertical scroll increments
        scrolldelay = setTimeout('pageScroll()',500); // scrolls every 500 milliseconds, controls scroll speed
    }
    </script> 

And instead of a plain html 'body' tag add the following to it

    <body onLoad="pageScroll()">

I have tested this code on desktop chrome and firefox browsers and it works fine, but on android devices the character encoding fails thereby leading to erratic display of the html content.

Upvotes: 1

Related Questions