how to display progress while loading a url to webview in android?

AndroidWebview

Android Problem Overview


I am loading url into webview:

WebView webview=(WebView)findViewById(R.id.webview); 
webview.loadUrl(url);

It's taking some time to load url, during which it shows a blank screen. I want to display a progress dialog while the url is loading:

ProgressDialog dialog = ProgressDialog.show(this, "HI","Loading......", true);

However, the above is code is not working. If any have any ideas, please help. Thanks in advance.

Android Solutions


Solution 1 - Android

set a WebViewClient to your WebView, start your progress dialog on you onCreate() method an dismiss it when the page has finished loading in onPageFinished(WebView view, String url)

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
 
public class Main extends Activity {
    private WebView webview;
    private static final String TAG = "Main";
    private ProgressDialog progressBar;  
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        requestWindowFeature(Window.FEATURE_NO_TITLE);
 
        setContentView(R.layout.main);
 
        this.webview = (WebView)findViewById(R.id.webview);
 
        WebSettings settings = webview.getSettings();
        settings.setJavaScriptEnabled(true);
        webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
 
        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
 
        progressBar = ProgressDialog.show(Main.this, "WebView Example", "Loading...");
 
        webview.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.i(TAG, "Processing webview url click...");
                view.loadUrl(url);
                return true;
            }
 
            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "Finished loading URL: " +url);
                if (progressBar.isShowing()) {
                    progressBar.dismiss();
                }
            }
 
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Log.e(TAG, "Error: " + description);
                Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
                alertDialog.setTitle("Error");
                alertDialog.setMessage(description);
                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        return;
                    }
                });
                alertDialog.show();
            }
        });
        webview.loadUrl("http://www.google.com");
    }
}

your main.xml layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <WebView android:id="@string/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" />
</LinearLayout>

Solution 2 - Android

You will have to over ride onPageStarted and onPageFinished callbacks

mWebView.setWebViewClient(new WebViewClient() {
		
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			if (progressBar!= null && progressBar.isShowing()) {
				progressBar.dismiss();
			}
			progressBar = ProgressDialog.show(WebViewActivity.this, "Application Name", "Loading...");
		}
		
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			
			return true;
		}

		public void onPageFinished(WebView view, String url) {
			if (progressBar.isShowing()) {
				progressBar.dismiss();
			}
		}

		public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
			alertDialog.setTitle("Error");
			alertDialog.setMessage(description);
			alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					return;
				}
			});
			alertDialog.show();
		}
	});

Solution 3 - Android

Check out the sample code. It help you.

 private ProgressBar progressBar;
 progressBar=(ProgressBar)findViewById(R.id.webloadProgressBar);
 WebView urlWebView= new WebView(Context);
    urlWebView.setWebViewClient(new AppWebViewClients(progressBar));
    					urlWebView.getSettings().setJavaScriptEnabled(true);
    					urlWebView.loadUrl(detailView.getUrl());

public class AppWebViewClients extends WebViewClient {
	 private ProgressBar progressBar;
	
	public AppWebViewClients(ProgressBar progressBar) {
		this.progressBar=progressBar;
		progressBar.setVisibility(View.VISIBLE);
	}
	@Override
	public boolean shouldOverrideUrlLoading(WebView view, String url) {
		// TODO Auto-generated method stub
		view.loadUrl(url);
		return true;
	}
	
	@Override
	public void onPageFinished(WebView view, String url) {
		// TODO Auto-generated method stub
		super.onPageFinished(view, url);
		progressBar.setVisibility(View.GONE);
	}
}

Thanks.

Solution 4 - Android

You need to set an own WebViewClient for your WebView by extending the WebViewClient class.

You need to implement the two methods onPageStarted (show here) and onPageFinished (dismiss here).

More guidance for this topic can be found in Google's WebView tutorial

Solution 5 - Android

  public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        alertDialog.setTitle("Error");
        alertDialog.setMessage(description);
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                return;
            }
        });
        alertDialog.show();
    }
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsaiView Question on Stackoverflow
Solution 1 - AndroidK_AnasView Answer on Stackoverflow
Solution 2 - AndroidMukund SamantView Answer on Stackoverflow
Solution 3 - AndroidMd Abdul GafurView Answer on Stackoverflow
Solution 4 - AndroidTimView Answer on Stackoverflow
Solution 5 - AndroidjohnView Answer on Stackoverflow