CoderzHeaven

November 3, 2011

How to show progress bar while loading a webpage in android?

This is a simple example showing how to load a webpage and show a progressBar while the page is loading in android.

For that you have to set a chrome client for the webview which implements “onProgressChanged”

Here is the layout file.

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><WebViewandroid:id="@+id/webview"android:layout_width="fill_parent"android:layout_height="fill_parent"></WebView></LinearLayout>

Here is the java file

package pack.coderzheaven;import android.app.Activity;import android.os.Bundle;import android.view.Window;import android.webkit.WebChromeClient;import android.webkit.WebView;public class LoadingWebViewDemo extends Activity {WebView webview;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);this.getWindow().requestFeature(Window.FEATURE_PROGRESS);setContentView(R.layout.main );// Makes Progress bar VisiblegetWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);webview = (WebView) findViewById( R.id.webview );webview.getSettings().setJavaScriptEnabled(true);webview.getSettings().setSupportZoom(true);//Zoom Control on webwebview.getSettings().setBuiltInZoomControls(true); //Enable Multitouch if supported by ROM// Load URLwebview.loadUrl("http://www.google.com");// This makes the Progress bar be updated.final Activity MyActivity = this;webview.setWebChromeClient(new WebChromeClient() {public void onProgressChanged(WebView view, int progress){MyActivity.setTitle("Loading...");MyActivity.setProgress(progress * 100);// Return the app name after finish loadingif(progress == 100)MyActivity.setTitle(R.string.app_name);}});}}
Website PinFacebook TwitterMyspaceFriendfeedTechnoratidel.icio.usDiggGoogleStumbleUponPremium Responsive

Leave a Reply Cancel reply