how to show progress bar on webview?

Here is the code that I am using:

Inside WebViewClient:

webView.setWebViewClient(new WebViewClient() {

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        findViewById(R.id.progress1).setVisibility(View.VISIBLE);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        findViewById(R.id.progress1).setVisibility(View.GONE);
    }

});

Here is the XML :

<ProgressBar
    android:id="@+id/progress1"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Hope this helps..


You need to do something like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_PROGRESS);
    this.setProgressBarVisibility(true);
}

And then

final Activity activity = this;
mWebView.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int progress) {
        activity.setProgress(progress * 100);
    }
});

Update
I know it is a bit too late but I have little side note: you shouldn't use setWebViewClient() twice. Second call cancels the first call so you wouldn't get error handling.


Try this:

getWindow().requestFeature(Window.FEATURE_PROGRESS);

WebView mWebView = (WebView) findViewById(R.id.mywebview);

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

1. In oncreate ur call AsyncTask.

2. In asynctask u just make progress dialog and show progress dialog.

3. In webview client u just show again progress dialog click on any link of web site which open in ur webview and after complete load link we override method onPageFinished and in this method we dismiss the progress dialog.

oncreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.web_view);

    web_view = (WebView) findViewById(R.id.web_view);

    OpenWebSiteInWebView opensite = new OpenWebSiteInWebView();
    opensite.execute();

}

AsyncTask

private class OpenWebSiteInWebView extends AsyncTask<String, Void, String> {

    @SuppressWarnings("deprecation")
    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected String doInBackground(String... params) {
        web_view.setWebViewClient(new MyWebViewClient());
        web_view.loadUrl("ur site name");
        return null;
    }

    @Override
    protected void onPostExecute(String result) {

    }

    @Override
    protected void onPreExecute() {

        pd = new ProgressDialog(SiteOpenInWebView.this);
        pd.setMessage("Please wait Loading...");
        pd.show();

    }

}

WebViewClient

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);

        if (!pd.isShowing()) {
            pd.show();
        }

        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        System.out.println("on finish");
        if (pd.isShowing()) {
            pd.dismiss();
        }

    }
}

Tags:

Android