Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WebView scroll to bottom

I try to scroll down the page I have in my WebView. Therefore I found the function .pageDown(true). The problem is that this function does not really work for me. Mostly it does not do anything.

Codesnippet:

wvChat.loadData(chat_, "text/html; charset=utf-8", "UTF-8");
                 wvChat.setWebViewClient(new WebViewClient() {
                     public void onPageFinished(WebView view, String url) {
                        wvChat.pageDown(true);
                     }
                 });

Is there another method or is it wrong to use it in onPageFinished?

like image 665
Phil Avatar asked Oct 26 '25 03:10

Phil


2 Answers

get the html content height and use scrollTo(x,y)

wvChat.loadData(chat_, "text/html; charset=utf-8", "UTF-8");
             wvChat.setWebViewClient(new WebViewClient() {
                 @Override
                 public void onPageFinished(WebView view, String url) {
                    //use the param "view", and call getContentHeight in scrollTo
                    view.scrollTo(0, view.getContentHeight());
                 }
             });
like image 189
petey Avatar answered Oct 28 '25 17:10

petey


I have a solution to scroll down large files (much lines) using the scrollTo function. I set a much higher value than the content height can be.

mWebView.setWebViewClient(new WebViewClient()
{
  @Override
  public void onPageFinished(WebView view, String url)
  {
    super.onPageFinished(view, url);     

    Handler lHandler = new Handler();
    lHandler.postDelayed(new Runnable()
    {             
      @Override
      public void run()
      {
        mWebView.scrollTo(0, 1000000000);
      }
    }, 200);
  } 
});

The problem is now, that it does not work for small files (less lines), because the onPageFinished method is called before the WebView has been rendered, so I have to use a delay handle.

like image 22
Mr. Fish Avatar answered Oct 28 '25 17:10

Mr. Fish