views:

396

answers:

3

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.

A: 

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));
Andy Zhang
A: 

: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

Jorgesys
That example did the trick, thanks!
brybam
A: 

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


}
Samuel