How do i make my progress dialog dismiss after webview is loaded?
Asked Answered
E

6

7

What do I need to my code to make the dialog dismiss() after the webview is loaded?

public void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            setContentView(R.layout.main); 
            CookieSyncManager.createInstance(this);
            CookieSyncManager.getInstance().startSync();
            webview = (WebView) findViewById(R.id.webview);
            webview.setWebViewClient(new homeClient());
            webview.getSettings().setJavaScriptEnabled(true);
            webview.getSettings().setPluginsEnabled(true);
            webview.loadUrl("http://google.com");

            ProgressDialog pd = ProgressDialog.show(Home.this, "", 
                    "Loading. Please wait...", true);

        }

I've tried

public void onPageFinshed(WebView view, String url){
        pd.dismiss();
    }

Didn't work.

Extrude answered 19/7, 2010 at 18:19 Comment(2)
Is onPageFinished called? In the ui thread?Cheung
Sorry I should have just done this in the first place, i put the whole Class up the way i current have it. and if you scroll up you can see I tried a onPageFinshed and it didnt work. maybe the way I did it was wrong? or where I placed it?Extrude
L
16

:o webview.setWebViewClient(new homeClient()); homeClient()????

try this

...
...
...

webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {              
                view.loadUrl(url);
                return true;
            }


          public void onPageFinished(WebView view, String url) {
                if (progressBar.isShowing()) {
                    progressBar.dismiss();
                }
            }
 webview.loadUrl("http://www.google.com");

}

Update:: this is a good example.

Android WebView and the Indeterminant Progress Solution

Lanky answered 19/7, 2010 at 18:43 Comment(0)
M
3

This is OK.

public class WordActivity extends Activity {

    private WebView webview;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
        Bundle objetbunble  = this.getIntent().getExtras(); 
        webview = (WebView) findViewById(R.id.webview);

        final Activity activity = this;

        webview.getSettings().setJavaScriptEnabled(true);

        webview.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {              
                view.loadUrl(url);
                return true;
            }
            public void onLoadResource (WebView view, String url) {
                if (progressDialog == null) {
                    progressDialog = new ProgressDialog(activity);
                    progressDialog.setMessage("Chargement en cours");
                    progressDialog.show();
                }
            }
            public void onPageFinished(WebView view, String url) {
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                    progressDialog = null;
                }
            }
        }); 
        webview.loadUrl("http://www.example.com");
    }


}
Milldam answered 22/10, 2010 at 0:16 Comment(1)
Hey samuel i have used ur code for webviews for initial launch every thing working fine. so when i navigate to same webview for second time progressdialog showing continously after pagefinish(note:waited for 10mins but progress dialog did not dismiss) Please help me how can i do tat.. thanks in advance...Grillroom
C
3

@Jorgesys this is not 100% accurate. If you have several iframes in a page you will have multiple onPageFinished (and onPageStarted). And if you have several redirects it may also fail. This approach i think solves all the problems:

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

    }
});
Casiano answered 2/3, 2011 at 20:23 Comment(0)
U
2

How are you accessing pd in onPageFinshed()? (And are you sure it's actually called when the page loads?)

In your onCreate(), try passing pd to your homeClient so that homeClient can take care of dismissing the dialog.

Your homeClient should look like this:

private class homeClient extends WebViewClient {

    private ProgressDialog pd;

    public homeClient(ProgressDialog pd) {
        this.pd = pd;
    }

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

    @Override
    public void onPageFinished (WebView view, String url) {
        if (pd.isShowing()) {
            pd.dismiss();
        }
    }
}

In your onCreate():

ProgressDialog pd = ProgressDialog.show(Fbook.this, "", 
                "Loading. Please wait...", true);
webview.setWebViewClient(new homeClient(pd));
Urology answered 19/7, 2010 at 18:42 Comment(2)
Very nice example, i like it!Ability
Cool...the part I've been missing for minutes now.Tecla
B
0

If you simply want to show Progress before webPage loading in WebView. You can simply request for Window Feature Progress like

getWindow().requestFeature(Window.FEATURE_PROGRESS); 

before setContentView(R.layout.blahblah);

and show it progress in onProgressChanged like

 final Activity context= this;
 webview.setWebChromeClient(new WebChromeClient() 
 {
   public void onProgressChanged(WebView webView, int progress) 
   {
     activity.setProgress(progress * 1000);
   }
 });

And if you want to add you own ProgressDialog then use WebviewClient

webView.setWebViewClient(new WebViewClient() {
    ProgressDialog rogressDialog ;
    @Override
    public void onPageStarted(WebView view, String url, Bitmap bitmap) {
        progressDialog = ProgressDialog.show(context, "Loading...", "Please wait...");//where context = YourActivity.this;
        super.onPageStarted(view, url, bitmap);
    }

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

webView.loadUrl(url);
Buckingham answered 5/8, 2015 at 8:8 Comment(0)
B
0
wv1.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(Entertainment.this, description, Toast.LENGTH_SHORT).show();
            }

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


            @Override
            public void onPageFinished(WebView view, String url) {
                progressDialog.dismiss();
                String webUrl = wv1.getUrl();

            }

        });

    wv1.loadUrl(url);

The above code will show a progress dialogue for every incoming link you navigate to the in webview.

Begat answered 23/5, 2016 at 16:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.