0

I know how to get notified when it finishes loading a web page, but is there any way to know when it starts loading a new one that is initiated from a link from the original web page?

The reason that i want it is to make a ProgressBar visible whenever a page starts loading, and make it invisible whenever it finishes.

UPDATE: What I'm asking is if it's possible to know when a new page starts loading a page. Although from the link I found onPageStarted, and it works.

SIMMORSAL
  • 1,402
  • 1
  • 16
  • 32
  • Possible duplicate of [How to listen for a WebView finishing loading a URL?](https://stackoverflow.com/questions/3149216/how-to-listen-for-a-webview-finishing-loading-a-url) – Yun CHEN Dec 25 '17 at 09:28

2 Answers2

0

Check with the following

 webView.setWebViewClient(new WebViewClient() {      


        //If you will not use this method url links are opeen in new brower not in webview
        public boolean shouldOverrideUrlLoading(WebView view, String url) {              
            view.loadUrl(url);
            return true;
        }


        public void onLoadResource (WebView view, String url) {
          //Show loader on url load
        }

        public void onPageFinished(WebView view, String url) {
           //cancel loader complted
        }

    }); 

     // Set Javascript enabled on webview  
    webView.getSettings().setJavaScriptEnabled(true); 
Anbarasu Chinna
  • 975
  • 9
  • 28
  • it already loads new links inside the `WebView` (although the method you're using is deprecated, and the new method returns `WebResourceRequest` instead of `String url`, and calling `getUrl` on the `request` only works in api 25+!!?), and `onLoadResource` is called multiple times during page load. i will go with `onPageStart`. thanks anyways – SIMMORSAL Dec 25 '17 at 10:19
0

To listen for a web page starting loading you should override onPageStarted, to listen for finishing you should override onPageFinished and just to make it more complete, listen for error with onReceivedError

webView.setWebViewClient(new WebViewClient() {  

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon){
            super.onPageStarted(view, url, favicon);

            // runs when a page starts loading
            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            // page finishes loading
            progressBar.setVisibility(View.GONE);
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);

            // runs when there's a failure in loading page
            progressBar.setVisibility(View.GONE);
            Toast.makeText(context, "Failure on loading web page", Toast.LENGTH_SHORT).show();
        }
}); 
SIMMORSAL
  • 1,402
  • 1
  • 16
  • 32
  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Rosário Pereira Fernandes Dec 25 '17 at 11:05