Shahal
Shahal

Reputation: 1070

Check Url completely loaded Webview

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

Upvotes: 2

Views: 962

Answers (2)

Peter Alwin
Peter Alwin

Reputation: 239

  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);
        }
    });

Upvotes: 0

Prajwal Waingankar
Prajwal Waingankar

Reputation: 2728

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.

Upvotes: 0

Related Questions