Jaison Brooks
Jaison Brooks

Reputation: 5826

Progress Bar in Webview

I am having troubles getting this progress bar to work properly. can any body help me with the source, is there something im missing?

 web = (WebView) findViewById(R.id.webview01);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        web.setWebViewClient(new myWebClient());

        //ADDING THIS RESOLVED THE PROGRESS BAR ACTUALLY LOADING.
        web.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
        progressBar.setProgress(progress); } 
        });
        //ADDING THIS RESOLVED THE PROGRESS BAR ACTUALLY LOADING.

        WebSettings websettings = web.getSettings();
        websettings.setBuiltInZoomControls(true);
        websettings.setUseWideViewPort(true);
        websettings.setJavaScriptEnabled(true);
        websettings.setAllowFileAccess(true);
        websettings.setDomStorageEnabled(true);
        websettings.setLoadWithOverviewMode(true);
        websettings.setSavePassword(true);
        web.loadUrl("http://www.google.com");

Then Later in the code

    //Loading the Actual Webpage
public class myWebClient extends WebViewClient
{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
        progressBar.setVisibility(View.VISIBLE);

    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub

        view.loadUrl(url);
        progressBar.setVisibility(View.VISIBLE);
        return true;

    }
    //web.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int progress) {
         progressBar.setProgress(progress);

    }




    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);

        progressBar.setVisibility(View.GONE);
    }

}

The webview itself works just fine, im just trying to have a horizontal loading bar, similar to browsers like dolphin, chrome, etc.

Here is the layout.

 <?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="@drawable/bgapp"
 android:fadingEdge="horizontal"
 android:gravity="center"
 android:orientation="vertical" >

 <include
    android:id="@+id/inc_blue"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    layout="@layout/inc_enphase_bar_with_title_buttons" />

<ProgressBar
    android:id="@+id/progressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:minHeight="8dip" />

<WebView
    android:id="@+id/webview01"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1.15"
    android:fadingEdge="vertical" >

</WebView>

Im not getting any errors or antying so no logcat, its just actually loading, it displays the progress bar when a webpage is loading but no animation or actually movement of loading.

Upvotes: 3

Views: 3119

Answers (1)

nandeesh
nandeesh

Reputation: 24820

You should also set intermediate progress by using

progressBar.setProgress(int);

for the progress to be shown. All you are doing is showing and hiding the view

Edit: Add below code.

   @Override
   public void onProgressChanged(WebView view, int progress) {
     progressBar.setProgress(progress);
   }

Sorry you need a webchromeclient for this. So use like below. Put it just below

web.setWebViewClient(new myWebClient());
//add this below line
web.setWebChromeClient(new WebChromeClient() {
   public void onProgressChanged(WebView view, int progress) {
        progressBar.setProgress(progress);
   }
 });

Check WebView Doc, it has an example.

Upvotes: 3

Related Questions