0

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

2 Answers2

2
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?

Community
  • 1
  • 1
neteinstein
  • 17,529
  • 11
  • 93
  • 123
  • Ok, when ever I try and use onPageStarted it keeps saying it's never used locally? I just can't figure out what I'm doing wrong! Although I'm sure it's bound to be something stupid... –  Sep 26 '11 at 16:29
  • I used progressBar.show(); and progressBar.hide(); in the annotation sections. –  Sep 26 '11 at 17:10
  • I'm not talking about that, in your code do you have @Override ? – neteinstein Sep 26 '11 at 17:45
  • Yes, although eclipse kept telling me to remove the override for onpagestarted, then when I do that it states it's not used locally. –  Sep 26 '11 at 23:21
  • Yeah, everything's imported that it needs. As my rep's too low I can't paste all my activity, so I had to start a new Q with the entire thing loaded. I think it'll be easier that way. –  Sep 27 '11 at 11:51
0

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

});
Ovidiu Latcu
  • 71,607
  • 15
  • 76
  • 84
  • Hi, thanks for the response. I've tried using onPageStarted as you've shown but eclipse keeps telling me it's not used locally. Not sure what I'm doing wrong.. –  Sep 26 '11 at 13:58