How to show a loading bar when rendering with Webview.loadUrl in android?-ThrowExceptions

Exception or error:

When using webview to show html pages,

there will be several seconds of blank page before it finished rendering.

For better user experience, is there a way to make it show a loading image during that several seconds?

How to solve:

Try this

        ...... 
        webView.setWebChromeClient(new WebChromeClient() {
           public void onProgressChanged(WebView view, int progress) {
              activity.setTitle("Loading...");
              activity.setProgress(progress * 100);

              if(progress == 100)
                 activity.setTitle(R.string.app_name);
           }
        });
        ...

###

Try this segment of code. Show a progressDialog or image or layout whatever you want in onPageStarted() & hide in onPageFinished()

        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon)
            {
                // TODO show you progress image
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url)
            {
                // TODO hide your progress image
                super.onPageFinished(view, url);
            }
        });

###

Try this code to show a ProgressDialog

    webView.setWebChromeClient(new WebChromeClient() {
        private ProgressDialog mProgress;

        @Override
        public void onProgressChanged(WebView view, int progress) {
            if (mProgress == null) {
                mProgress = new ProgressDialog(getActivity());
                mProgress.show();
            }
            mProgress.setMessage("Loading " + String.valueOf(progress) + "%");
            if (progress == 100) {
                mProgress.dismiss();
                mProgress = null;
            }
        }
    });

###

Simple code for Java–

 webView.setWebViewClient(new WebViewClient{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        pdialog.show();
    }
    @Override
    public void onPageFinished(WebView view, String url)
    {
        pdialog.hide();
    });

###

This is what worked for me in KOTLIN:

private fun setupWebView() {

    val webViewClient: WebViewClient = object: WebViewClient() {

        override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
            view?.loadUrl(request?.url.toString())
            return super.shouldOverrideUrlLoading(view, request)
        }

        override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
            showProgressDialog()
            super.onPageStarted(view, url, favicon)
        }

        override fun onPageFinished(view: WebView?, url: String?) {
            hideProgressDialog()
            super.onPageFinished(view, url)
        }
    }
    webView.webViewClient = webViewClient

    webView.settings.defaultTextEncodingName = "utf-8"
}

Leave a Reply

Your email address will not be published. Required fields are marked *