How to show a progress dialog while HTML page is loaded in WebView
Asked Answered
O

4

6

I am using the web view for showing html pages, and I want to show a progress dialog until the page is loaded. When that is done, the dialog has to disappear. I have used AsyncTask for this, but the dialog doesn't show. See my code below:

 class DownloadAysnc extends AsyncTask<String, String, Void>
   {
    ProgressDialog progressDialog;
        @Override
          protected void onPreExecute() {
          super.onPreExecute();
          progressDialog = ProgressDialog.show(OverView.this, "", "Please Wait ...");
        }

        @Override
          protected Void doInBackground(String... arg0) {
          webView.loadUrl("http://marico.com/html/investor/overview.php");
        return null;
       }

        @Override
          protected void onPostExecute(Void result){
          super.onPostExecute(result);
          progressDialog.dismiss();
       }
   }

And if I take the help of google docs to show the web page, then the HTML Tag is shown, but not the page. Below is that code:

String url = "http://google.co.in/";
String googleDocsUrl = "http://docs.google.com/viewer?url="+url;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(googleDocsUrl ), "text/html");
startActivity(intent);

this.myWebView.loadUrl(googleDocsUrl);  

Can someone help me with this?

Onwards answered 7/2, 2012 at 5:40 Comment(0)
C
13

Use this code:

webView.setWebViewClient(new WebViewClient() {
    ProgressDialog prDialog;
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        prDialog = ProgressDialog.show(Activity.this, null, "loading, please wait...");
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        prDialog.dismiss();
        super.onPageFinished(view, url);
    }
});

webView.loadUrl(url);
Councilwoman answered 7/2, 2012 at 5:49 Comment(2)
what should I put (WebView view, String url, Bitmap favicon) It is giving me the Compile time error at the Bitmap favicon line . What should I put their.Onwards
It gives me an error at Bitmap favicon. What should I do ?Hyphen
F
3
  v.setWebChromeClient(new WebChromeClient(){
      @Override
      public void onProgressChanged(WebView view, int newProgress) {
          WebView v=(WebView)findViewById(R.id.wv);
          //Toast.makeText(mContext, v.getUrl() + newProgress +"uploading...", Toast.LENGTH_SHORT).show();
          ProgressBar s=(ProgressBar)findViewById(R.id.progressBar1);
          s.setMax(100);
          s.setProgress(newProgress);
          if(newProgress==100){                        
              v.setVisibility(0);
              //Toast.makeText(mContext, "upload finished...", Toast.LENGTH_SHORT).show();
         }else{
              v.setVisibility(8);
              //Toast.makeText(mContext, "uploading...", Toast.LENGTH_SHORT).show();
          }
        }
    });
Fabric answered 21/11, 2012 at 2:19 Comment(2)
Welcome to Stack Overflow. Can you please elaborate more your answer.Biggers
Especially when you answer an old question that already has accepted and upvoted answers, provide some explanation. How is your answer different, and better, than the previously accepted answer?Osteo
E
2

You can show the Progress of the WebView in the Title Bar of the WebView. Here is a complete example for the same that show Progress Status in the Title Bar of the WebView.

Epirogeny answered 7/2, 2012 at 5:54 Comment(0)
C
1

You can try following code,

progDailog = ProgressDialog.show(loginAct,"Process ", "please wait....",true,true);

new Thread ( new Runnable()
{
     public void run()
     {
      // your code goes here
     }
}).start();

 Handler progressHandler = new Handler() 
 {

     public void handleMessage(Message msg1) 
     {
         progDailog.dismiss();
     }
 }
Counterweight answered 7/2, 2012 at 5:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.