Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

show progress dialog on webview

Tags:

android

In my android application i am opening a webview.I want to hide url that is getting loaded,so the default window progress bar doesnot work for me.

Is there any way than that i can add progress dialogue on webview.

I am using the below code.

 mWebView = (WebView) findViewById(R.id.webview);
     mWebView.getSettings().setJavaScriptEnabled(true); 

     final Activity activity = this; 

     mWebView.setWebChromeClient(new WebChromeClient(){ 

     public void onProgressChanged(WebView view, int progress) { 
     activity.setTitle("Loading..."); 
     activity.setProgress(progress * 100); 
     if(progress == 100) 
     activity.setTitle("My title"); 
     } 
     }); 

     mWebView.loadUrl(Url);
     mWebView.setWebViewClient(new HelloWebViewClient());
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

}

private class HelloWebViewClient extends WebViewClient {   
    ProgressDialog MyDialog=new ProgressDialog(context);

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

Please share your valuable suggestions.

Thanks in advance :)

like image 267
Remmyabhavan Avatar asked Feb 14 '11 03:02

Remmyabhavan


1 Answers

if you hide application title by "android:theme="@android:style/Theme.NoTitleBar"" from manifest then you won't see progress bar which is being displayed in title by this code:

    this.getWindow().requestFeature(Window.FEATURE_PROGRESS);               
    setContentView(R.layout.main);

// Makes Progress bar Visible
getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);

So you can show progress bar with a Dialog box like this inside the onCreate():

final Activity activity = this;

final ProgressDialog progressDialog = new ProgressDialog(activity);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(false);

browser.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int progress) {
        progressDialog.show();
        progressDialog.setProgress(0);
        activity.setProgress(progress * 1000);

        progressDialog.incrementProgressBy(progress);

        if(progress == 100 && progressDialog.isShowing())
            progressDialog.dismiss();
    }
});
like image 126
Hakan Cakirkan Avatar answered Sep 25 '22 19:09

Hakan Cakirkan