Tuesday, August 14, 2012

Display Progress Bar on WebView when loading in Android

Continuous work on last post Enable JavaScript and built-in zoom control of WebView; to display progress bar on WebView when loading:

Display Progress Bar on WebView when loading

- Request feature of Window.FEATURE_PROGRESS:
Add the code getWindow().requestFeature(Window.FEATURE_PROGRESS) before setContentView().

- Implement your custom WebChromeClient class with onProgressChanged() method overrided, to call setProgress() method.

- Updated onCreate() should like this:
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.main);
        
        webView = (WebView)findViewById(R.id.webview);
  webView.getSettings().setJavaScriptEnabled(true);
  webView.getSettings().setBuiltInZoomControls(true);
        webView.setWebViewClient(new MyWebViewClient());
        webView.loadUrl(DEFAULT_URL);
        
        final Activity activity = this;
  webView.setWebChromeClient(new WebChromeClient() {
   public void onProgressChanged(WebView view, int progress) {
    // Activities and WebViews measure progress with different scales.
    // The progress meter will automatically disappear when we reach 100%
    activity.setProgress(progress * 100);
    
   }});
        
    }

No comments:

Post a Comment