2

I want to show a progressBar till all resources are loaded. I've tried below code, but progressBar will go off when resources starts loading.

dataBinding.webviewGallery.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (!loadingFinished) {
                redirect = true;
            }
            return false;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            loadingFinished = false;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            if (!redirect) {
                loadingFinished = true;
            }

            if (loadingFinished && !redirect && !loadingError) {
                dataBinding.progressBar.setVisibility(View.GONE);
            } else {
                redirect = false;
            }
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            dataBinding.progressBar.setVisibility(View.GONE);
            loadingError = true;
            Toast.makeText(requireActivity(), "Failed to load URL. Please try again later.", Toast.LENGTH_SHORT).show();
            goHome();
        }
    });

I've also tried WebChromeClient, which is also not working.

dataBinding.webviewGallery.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                dataBinding.progressbarWebpage.setProgress(newProgress);
                if(newProgress==100){
                    dataBinding.progressbarWebpage.setVisibility(View.GONE);
                }
            }
        });

Is there a proper way to find when URL is completely loaded ? TIA

Shahal
  • 1,008
  • 1
  • 11
  • 29

2 Answers2

0

The reason of your issue is because you are writing the code inside on your onProgressChanged(). This function will get executed whenever there is a change in the progress. In your case the inital value of progress is 0 since the progress is not started. When the progresd gets started i.e. the progress value changes, the functiom gets executed and hence, the progress is gone. Solution: add your progress logic withing onprogresscompleted() function.

Prajwal Waingankar
  • 2,534
  • 2
  • 13
  • 20
  • There's no onprogresscompleted() to WebChromeClient!! And i hide the progressBar only when current progress is equal to 100!! – Shahal Nov 26 '19 at 11:05
  • Check whether there is any oncomplete() for it. Onchanged is definitely the wrong function to add your code irrespective of your logic – Prajwal Waingankar Nov 26 '19 at 15:55
0
  binding.webView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            if (progress == 100)
                binding.progress.setVisibility(View.GONE);
            else
                binding.progress.setProgress(progress);
        }
    });
Peter Alwin
  • 239
  • 1
  • 6