Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WebView progress bar

I have looked at a question similar to this here but as I am a newbie could someone explain how to get this to work in a WebView or at least how to set a 10 second time delay so people know that it's loading?

like image 693
Max Marroni Avatar asked Mar 29 '10 11:03

Max Marroni


People also ask

What is ProgressBar in Android?

Android ProgressBar is a graphical view indicator that shows some progress. Android progress bar displays a bar representing the completing of the task. Progress bar in android is useful since it gives the user an idea of time to finish its task.

What is progress dialog in Android?

Android ProgressDialog is a dialog box/dialog window which shows the progress of a task. Android Progress Dialog is almost same as ProgressBar with the exception that this is displayed as a dialog box. In order to create a ProgressDialog to display a ProgressBar we need to instantiate it like this.


2 Answers

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.

like image 95
Yahyaotaif Avatar answered Sep 25 '22 16:09

Yahyaotaif


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); 
like image 34
Max Marroni Avatar answered Sep 21 '22 16:09

Max Marroni