mayur rahatekar
mayur rahatekar

Reputation: 4460

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

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?

Upvotes: 6

Views: 3732

Answers (4)

rainbow
rainbow

Reputation: 43

  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();
          }
        }
    });

Upvotes: 3

Lalit Poptani
Lalit Poptani

Reputation: 67286

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.

Upvotes: 2

Lucifer
Lucifer

Reputation: 29632

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();
     }
 }

Upvotes: 1

Jin35
Jin35

Reputation: 8612

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);

Upvotes: 13

Related Questions