0

In my app I'm loading a local HTML file to a webview. If some button was clicked, I want to load a local HTML file and scroll it with specific value in y axis.

The problem is that the command:

webview.scrollTo(0, scrollY);

is executed too early, before the HTML file finish loaded, so the scrolling is not happen. I tried to use a lot of answers from Stackoverflow (like this, and this) but the problem is always the same. I can see in debug mode that the scrollTo command executed before the file is visible.

Anyone have any idea how can I achieved this scrolling?


After trying all the answers I found that only the following really help: private final Handler mHandler = new Handler();

    // in onCreate
    mHandler.postDelayed(new Runnable() {

            @Override
            public void run() {
                if (mWebView.getContentHeight() > 0) {
                    mWebView.scrollTo(0, mLastPosition);
                    Log.d("scrolling", "true");
                    mHandler.removeCallbacks(this);
                } else {
                    mHandler.postDelayed(this, 100);
                }
            }
        }, 100);

This solution was taken from hereenter link description here

Community
  • 1
  • 1
RafaelJan
  • 3,118
  • 1
  • 28
  • 46
  • try:1. put a delay before scrollTo is executed, 2. put the code in your local HTML to scroll parent window – Godinall Dec 29 '13 at 07:52
  • 1. delay of how much time? 2. I don't want to put it in the HTML file since it is always changed according to the user behavior. The purpose is to get the user into the last location that he was in this HTML file. This is a book app. – RafaelJan Dec 29 '13 at 09:01
  • I'd say 3 seconds most as for user friendly, you'd better use a ajax loader indicating that the page is loading not freezing; 2. you can write a generic function say do_scroll(height) and call the different height variables if you put it in the HTML file, what you think? – Godinall Dec 29 '13 at 09:20
  • i think u need to go for this. http://www.mediacollege.com/internet/javascript/page/scroll.html – Sush Dec 30 '13 at 04:19
  • Interesting. I will check it. Thanks – RafaelJan Dec 30 '13 at 10:35

2 Answers2

0

WebViewClient will help you. Extend onPageFinished() as follows:

webview.setWebViewClient(new WebViewClient() {

   public void onPageFinished(WebView view, String url) {
       webview.scrollTo(0, scrollY);
    }
});
Nizam
  • 5,698
  • 9
  • 45
  • 57
0
fisrt set custom webview client like this
webview.setWebViewClient(new CustomWebViewClient());

then add following piece of code in class

 private class CustomWebViewClient extends WebViewClient
{
public boolean shouldOverrideUrlLoading(WebView view, String url) 
{
    // TODO Auto-generated method stub

        return false;
} 
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}

@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
  webview.scrollTo(0, scrollY);

}


}
Sush
  • 3,864
  • 2
  • 17
  • 35
  • Unfortunately, it doesn't help. The command onPageFinished is running before the file loaded, so the scroll operation doesn't occur. – RafaelJan Dec 29 '13 at 21:58