comment afficher la progression lors du chargement d’une URL dans la vue Web dans Android?

Je charge url dans webview:

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

Il faut un certain temps pour charger url, pendant lequel il affiche un écran vide. Je souhaite afficher une boîte de dialog de progression pendant le chargement de l’URL:

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

Cependant, le code ci-dessus ne fonctionne pas. Si vous avez des idées, aidez-nous. Merci d’avance.

définir un WebViewClient sur votre WebView, démarrez votre boîte de dialog de progression sur la méthode onPageFinished(WebView view, Ssortingng 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 Ssortingng 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, Ssortingng url) { Log.i(TAG, "Processing webview url click..."); view.loadUrl(url); return true; } public void onPageFinished(WebView view, Ssortingng url) { Log.i(TAG, "Finished loading URL: " +url); if (progressBar.isShowing()) { progressBar.dismiss(); } } public void onReceivedError(WebView view, int errorCode, Ssortingng description, Ssortingng 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"); } } 

votre mise en page main.xml

     

Vous devrez dépasser les callbacks onPageStarted et onPageFinished

 mWebView.setWebViewClient(new WebViewClient() { public void onPageStarted(WebView view, Ssortingng url, Bitmap favicon) { if (progressBar!= null && progressBar.isShowing()) { progressBar.dismiss(); } progressBar = ProgressDialog.show(WebViewActivity.this, "Application Name", "Loading..."); } public boolean shouldOverrideUrlLoading(WebView view, Ssortingng url) { view.loadUrl(url); return true; } public void onPageFinished(WebView view, Ssortingng url) { if (progressBar.isShowing()) { progressBar.dismiss(); } } public void onReceivedError(WebView view, int errorCode, Ssortingng description, Ssortingng failingUrl) { alertDialog.setTitle("Error"); alertDialog.setMessage(description); alertDialog.setButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { return; } }); alertDialog.show(); } }); 

Consultez le code exemple. Ça vous aide

  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, Ssortingng url) { // TODO Auto-generated method stub view.loadUrl(url); return true; } @Override public void onPageFinished(WebView view, Ssortingng url) { // TODO Auto-generated method stub super.onPageFinished(view, url); progressBar.setVisibility(View.GONE); } } 

Merci.

Vous devez définir un propre WebViewClient pour votre WebView en étendant la classe WebViewClient .

Vous devez implémenter les deux méthodes onPageStarted (afficher ici) et onPageFinished (rejeter ici).

Vous trouverez plus de conseils sur ce sujet dans le didacticiel WebView de Google.

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