Android WebView progress bar Android WebView progress bar android android

Android WebView progress bar


For a horizontal progress bar, you first need to define your progress bar and link it with your XML file like this, in the onCreate:

final TextView txtview = (TextView)findViewById(R.id.tV1);final ProgressBar pbar = (ProgressBar) findViewById(R.id.pB1);

Then, you may use onProgressChanged Method in your WebChromeClient:

MyView.setWebChromeClient(new WebChromeClient() {            public void onProgressChanged(WebView view, int progress) {               if(progress < 100 && pbar.getVisibility() == ProgressBar.GONE){                   pbar.setVisibility(ProgressBar.VISIBLE);                   txtview.setVisibility(View.VISIBLE);               }               pbar.setProgress(progress);               if(progress == 100) {                   pbar.setVisibility(ProgressBar.GONE);                   txtview.setVisibility(View.GONE);               }            }        });

After that, in your layout you have something like this

<TextView android:text="Loading, . . ."     android:textAppearance="?android:attr/textAppearanceSmall"    android:id="@+id/tV1" android:layout_height="wrap_content"    android:layout_width="wrap_content"    android:textColor="#000000"></TextView><ProgressBar android:id="@+id/pB1"    style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent"    android:layout_height="wrap_content" android:layout_centerVertical="true"    android:padding="2dip"></ProgressBar>

This is how I did it in my app.


I have just found a really good example of how to do this here:http://developer.android.com/reference/android/webkit/WebView.html .You just need to change the setprogress from:

activity.setProgress(progress * 1000);

to

activity.setProgress(progress * 100);


here is the easiest way to add progress bar in android Web View.

Add a boolean field in your activity/fragment

private boolean isRedirected;

This boolean will prevent redirection of web pages cause of dead links.Now you can just pass your WebView object and web Url into this method.

private void startWebView(WebView webView,String url) {    webView.setWebViewClient(new WebViewClient() {        ProgressDialog progressDialog;        public boolean shouldOverrideUrlLoading(WebView view, String url) {            view.loadUrl(url);            isRedirected = true;            return false;        }        @Override        public void onPageStarted(WebView view, String url, Bitmap favicon) {            super.onPageStarted(view, url, favicon);            isRedirected = false;        }        public void onLoadResource (WebView view, String url) {            if (!isRedirected) {                if (progressDialog == null) {                    progressDialog = new ProgressDialog(SponceredDetailsActivity.this);                    progressDialog.setMessage("Loading...");                    progressDialog.show();                }            }        }        public void onPageFinished(WebView view, String url) {            try{                isRedirected=true;                if (progressDialog.isShowing()) {                    progressDialog.dismiss();                    progressDialog = null;                }            }catch(Exception exception){                exception.printStackTrace();            }        }    });    webView.getSettings().setJavaScriptEnabled(true);    webView.loadUrl(url);}

Here when start loading it will call onPageStarted. Here i setting Boolean field is false. But when page load finish it will come to onPageFinished method and here Boolean field is set to true. Sometimes if url is dead it will redirected and it will come to onLoadResource() before onPageFinished method. For this reason it will not hiding the progress bar. To prevent this i am checking if (!isRedirected) in onLoadResource()

in onPageFinished() method before dismissing the Progress Dialog you can write your 10 second time delay code

That's it. Happy coding :)