简体   繁体   中英

Webview loading dialog

My code below loads a progress dialogue into webview on the initial load but I'd like it to appear every time webview is loading a URL!

I think I need to use onpagestarted() before the progressbar but I'm a bit of a noob at all this and having trouble getting it working. Can anyone advise please?

    //need to set progress bar condition here!

    progressBar = ProgressDialog.show(MyActivity.this, "Loading title", "Loading...");

    myWebView.setWebViewClient(new WebViewClient() {

         public void onPageFinished(WebView view, String url) {
             Log.i(TAG, "Finished loading URL: " +url);
             if (progressBar.isShowing()) {
                 progressBar.dismiss();
             }
         }

    });
    //web site load
    myWebView.loadUrl("http://www.google.com");
boolean loadingFinished = true;
boolean redirect = false;

mWebView.setWebViewClient(new WebViewClient() {

   @Override
   public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
       if (!loadingFinished) {
          redirect = true;
       }

   loadingFinished = false;
   webView.loadUrl(urlNewString);
   return true;
   }

   @Override
   public void onPageStarted(WebView view, String url) {
        loadingFinished = false;
        //SHOW LOADING IF IT ISNT ALREADY VISIBLE  
    }

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

       if(loadingFinished && !redirect){
         //HIDE LOADING IT HAS FINISHED
       } else{
          redirect = false; 
       }

    }
});

As shown here: How to listen for a WebView finishing loading a URL?

You should use the onPageStarted() method to show the ProgressDialog :

myWebView.setWebViewClient(new WebViewClient() {

     public boolean shouldOverrideUrlLoading(WebView view, String url) {
          view.loadUrl(url);
          return false;
     }

     public void onPageStarted(WebView view, String url, Bitmap favicon) {
          progressBar.show();
     }

     public void onPageFinished(WebView view, String url) {
          progressBar.hide();
     }

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM