WebView onProgressChanged() not called

9k Views Asked by At

I have a WebView which loads a URL all well & good. I have gotten a WebViewClient and extended onProgressChanged, but as far as I can tell it is not being called. Any ideas?

 wv.setWebViewClient(new WebViewClient(){

        public void onProgressChanged(final WebView view, final int newProgress) {
            Log.e("APPNAME", String.valueOf(newProgress));
            if (newProgress < 100 && progressBar.getVisibility() == ProgressBar.GONE){
                progressBar.setVisibility(ProgressBar.VISIBLE);
            }
            progressBar.setProgress(newProgress);
            progressTxt.setText(newProgress);
            if (newProgress == 100){
                progressBar.setVisibility(ProgressBar.GONE);
            }
        }

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
             InputStream is = null;
             byte[] buffer = null;
                try {
                    is = getAssets().open("error.html");
                    int size = 0;
                    size = is.available();
                    buffer = new byte[size];
                    is.read(buffer);
                    is.close();
                }
                catch(Exception e){

                }
                String str = new String(buffer);
                str = str.replace("%@", description);

                view.loadDataWithBaseURL("file:///android_asset/", str, "text/html", "utf-8", null); 
        }
    });
3

There are 3 best solutions below

0
On BEST ANSWER

Try this

You are using wrong webClient

wv.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int newProgress) {
                progressBar.setProgress(newProgress);
            }
        });
0
On

WebView client does not have any method called onProgressChanged. You can check it here

Move your code into webchrome client which supports this method. Check here

1
On

use webChromeClient instead of webViewClient