Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll WebView to top in android?

I am working on a webView in an app, where content which webview load changes when a button is pressed (two buttons next and previous , they just change the content in webview).

If I scroll down to a point in webview and press next or previous button it starts from the same point. I just want that everytime the new data is loaded to webview it display the content from the top. I have used:

webView.scrollTo(0, 0);

But doesnt work.How can it work ?Please help anyone?

like image 837
Navdroid Avatar asked Oct 05 '12 06:10

Navdroid


2 Answers

You can use a webviewclient and set the onPageFinished to scroll to the top: How to listen for a WebView finishing loading a URL?

mWebView.setWebViewClient(new WebViewClient() {
  public void onPageFinished(WebView view, String url) {
    view.scrollTo(0,0);
  }
});

That way, the content will be loaded when you scroll. It may feel a bit sluggish, but it should work every time.

like image 136
seanmakesgames Avatar answered Oct 06 '22 01:10

seanmakesgames


Maybe delaying it would work? as the content doesn't usualy load instantly

new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                webView.scrollTo(0,0);

            }
        }, 500);
like image 23
sokie Avatar answered Oct 06 '22 00:10

sokie