Rob
Rob

Reputation: 15982

My ProgressBar won't show while loading my WebView, why?

I have created a WebViewActivity extending Activity and showing a WebView inside of my app instead of redirecting to the browser.

So I tried to use AsyncTask to show my ProgressBar while the view is loading. But the progress bar doesn't display at all. Here is the code I'm using:

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.app.Activity;

public class WebViewActivity extends Activity {

    WebView webView;
    TextView textView;
    ProgressBar bar;

    private class Async extends AsyncTask<Void, Void, Void> {

        protected Void doInBackground(Void... arg0) {
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            String url = new String(getIntent().getStringExtra("url"));
            webView.getSettings().setLoadWithOverviewMode(true);
            webView.getSettings().setUseWideViewPort(true);
            webView.loadUrl(url);
            textView.setText(getIntent().getStringExtra("title"));
            bar.setVisibility(ProgressBar.INVISIBLE);
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_web_view);

        vueText = (TextView) findViewById(R.id.barTitle);
        vueWeb = (WebView) findViewById(R.id.WebView);
        bar = (ProgressBar) findViewById(R.id.progressBar);
        bar.setVisibility(ProgressBar.VISIBLE);
        new Async().execute();
    }
}

And here is my layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/grayBackground"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/barTitle" />

    <ScrollView
        android:id="@+id/sc1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/barTitle"
        android:fillViewport="true" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <WebView
                android:id="@+id/WebView"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </ScrollView>

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:visibility="visible" />

</RelativeLayout>

Upvotes: 0

Views: 873

Answers (2)

Johan
Johan

Reputation: 318

You should perform the background action (loading the URL) of the AsyncTask in the doInBackground() method.

The onPostExecute() is invoked on the UI thread so it will freeze the UI while running.

See the documentation of AsyncTask for more details.

Upvotes: 2

Lisa Anne
Lisa Anne

Reputation: 4595

You have to use pubishProgress() inside the doInBackground.

Then override onProgressUpdate

Upvotes: 1

Related Questions