0

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>
Rob
  • 15,732
  • 22
  • 69
  • 107

2 Answers2

2

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.

Johan
  • 318
  • 2
  • 9
1

You have to use pubishProgress() inside the doInBackground.

Then override onProgressUpdate

Lisa Anne
  • 4,482
  • 17
  • 83
  • 157
  • Edit to `publishProgress()` – hakki Mar 06 '13 at 20:45
  • publishProgress() is only if you want to react on partial progress. For example if you have a linear progress-bar that you want to represent how much work is left. – Johan Mar 06 '13 at 20:46