Jack Dsilva
Jack Dsilva

Reputation: 1514

How to show a progress bar for a layout until the WebView is loaded

I am loading a WebView to the LinearLayout and want to display a ProgressBar inside this layout until the WebView is fully loaded.I wnet through the below links and they seems to be really great but I do not want to use ProgressDialog, instead I would like to use progressbar inside the layout.

http://xjaphx.wordpress.com/2011/07/31/load-webview-with-progressdialog/

how to show progress bar on webview?

How would I do that?

Say for example,

if(pos==0)
{
 mLayout.setVisibility(View.VISIBLE);
 mWeb.loadUrl("http://android.stackexchange.com/");
}

Inside mLayout I would like to display a progressbar until the page gets displayed.

Upvotes: 2

Views: 5367

Answers (3)

loshkin
loshkin

Reputation: 1630

I am sure this is a better way ti detect that a page loading is finished.

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            loadingIndicator.setVisibility(View.GONE);
        }
    });

Upvotes: 0

Vineet Shukla
Vineet Shukla

Reputation: 24021

To define progress bar inside webview:

1. Define ProgressBar and Webview inside your layout like this:

<ProgressBar android:id="@+id/progressbar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="8px"
        android:max="100"
        android:visibility="gone" />

<ScrollView
    android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fadingEdge="none" >

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

</ScrollView>

2. Inside your activity:

    progressBar = (ProgressBar)findViewById(R.id.progressbar);

    progressBar.setProgress(0);

    progressBar.setVisibility(View.VISIBLE);

    WebView mWebView = (WebView)findViewById(R.id.webkit);

    mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

    mWebView.setWebChromeClient(new WebChromeClient(){

    public void onProgressChanged(WebView view, int progress) {

         progressBar.setProgress(progress);
         if(progress == 100) {
             progressBar.setVisibility(View.GONE);
          }
       }
    });

Upvotes: 7

Cata
Cata

Reputation: 11211

You can simply use the webView setPictureListener() like this:

// here you can start a loading wheel

   webView.setPictureListener(new WebView.PictureListener() {
      public void onNewPicture(WebView webView, Picture picture) {
           // close your loading wheel here
            }
        });

   //load your url
   webView.loadUrl(yourUrl);

Upvotes: 3

Related Questions