Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 898
brian Avatar asked Mar 22 '10 22:03

brian


People also ask

Can you use to display a progress bar in an android application?

Progress bars are used to show progress of a task. For example, when you are uploading or downloading something from the internet, it is better to show the progress of download/upload to the user. In android there is a class called ProgressDialog that allows you to create progress bar.

How can I set progress bar progress in android?

You can update the percentage of progress displayed by using the setProgress(int) method, or by calling incrementProgressBy(int) to increase the current progress completed by a specified amount. By default, the progress bar is full when the progress value reaches 100.


2 Answers

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);
  }
}
like image 60
beetstra Avatar answered Nov 15 '22 04:11

beetstra


There's a really good tutorial on the Android Developers website for that. It shows how to create the 'spinning wheel' progress dialog used throughout Android programs, and even some basics on how to handle loading in a separate thread to prevent your application from freezing while loading.

like image 35
Steve Haley Avatar answered Nov 15 '22 04:11

Steve Haley