Show a progress bar on a child tab until the WebView loads

brian picture brian · Mar 22, 2010 · Viewed 13.2k times · Source

In an Android app I am using a TabView and one of the tabs shows a WebView. But the page is blank until the web page loads. How would one show a progress bar until the page loads? It cannot be in the title bar because that is hidden by the tab host.

Answer

beetstra picture beetstra · Dec 2, 2011

I use a ProgressBar for this. With a layout like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout [...]>

  <WebView 
        android:id="@+id/WebView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

  <ProgressBar 
       android:id="@+id/ProgressBar"
       android:layout_centerInParent="true"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       style="?android:attr/progressBarStyleLarge"
       android:visibility="gone"/>
</RelativeLayout>

I hide and show the progress indicator using:

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

 final ProgressBar progess = (ProgressBar) findViewById(R.id.ProgressBar);

  webView.setWebViewClient(new WebViewClient() {
  public void onPageStarted(WebView view, String url, Bitmap favicon) {
    progess.setVisibility(View.VISIBLE);
  }

  public void onPageFinished(WebView view, String url) {
    progess.setVisibility(View.GONE);
  }
}