Java – Android webview scrolling does not work

Android webview scrolling does not work… here is a solution to the problem.

Android webview scrolling does not work

I

tried to scroll down to the bottom of the page in WebView, and I was using the WebView example that Google provided in its tutorial. I’m using this line of code trying scrolling but it doesn’t work. mWebView.pageDown(true);
Any suggestions on how to scroll programmatically? Thank you.

public class WebViewExample extends Activity {

private static final String LOG_TAG = "WebViewDemo";

private WebView mWebView;

private Handler mHandler = new Handler();

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    mWebView = (WebView) findViewById(R.id.webview);

WebSettings webSettings = mWebView.getSettings();
    webSettings.setSavePassword(false);
    webSettings.setSaveFormData(false);
    webSettings.setJavaScriptEnabled(true);
    webSettings.setSupportZoom(false);

mWebView.setWebChromeClient(new MyWebChromeClient());

mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "demo");

mWebView. loadUrl(" http://www.google.com/search?q=android+webview+scroll+doesnt+work&hl=en&safe=off&prmd=ivns&ei=0b0cTquNNI6WsgP6l7igBQ&start=10&sa=N#sclient=psy&hl=en&safe=off&source=hp&q=android%20webview%20scrollable&pbx=1&oq=&aq=&aqi=&aql=&gs_sm=&gs_upl=&bav=on.2,or.r_gc.r_pw.&fp=c306e119a7d85f91&biw=1280&bih=738&pf=p&pdl=300 ");

this is where I try to scroll, doesn't work
           mWebView.pageDown(true);

}

final class DemoJavaScriptInterface {

DemoJavaScriptInterface() {
    }

/**
     * This is not called on the UI thread. Post a runnable to invoke loadUrl on the UI thread.
     */
    public void clickOnAndroid() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mWebView.loadUrl("javascript:wave()");
            }
        });

}
}

/**
 * Provides a hook for calling "alert" from javascript. Useful for debugging your javascript.
 */
final class MyWebChromeClient extends WebChromeClient {
    @Override
    public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
        Log.d(LOG_TAG, message);
        result.confirm();
        return true;
    }
}

Solution

I think mWebView.pageDown(true); Should be called after the content is loaded.

mWebview.setWebViewClient(new WebViewClient() {
    public void  onPageFinished (WebView view, String url) {
      mWebView.pageDown(true);
    }
});

Related Problems and Solutions