How to display progress when loading url in webview in Android?

I load url in webview:

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

It will take some time to load the URL during which it displays a blank screen. I want to display a progress dialog while loading the URL:

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

However, the above code does not work. If you have any ideas, please help. Thank you in advance.

+59
android webview
Jul 02 '12 at 6:20
source share
5 answers

set a WebViewClient to your WebView, start the execution dialog on your onCreate() method and release it when the page finishes 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> 
+111
Jul 02 '12 at 6:26
source share

You will have to overcome 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(); } }); 
+8
Jul 02 '12 at 6:29
source share

Check sample code. This will 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); } } 

Thank.

+3
Jul 02 '12 at 6:27
source share

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

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

Further guidance on this topic can be found in the Google WebView Tutorial.

+1
Jul 02 '12 at 6:25
source share
  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(); } }); 
-one
May 03 '13 at 6:27
source share



All Articles