开发者

Need to Show Progress Dialog on WebView Initial Page Load

I'm working on a simple android app that loads a single website using the Android Level 8 api. I'm coding with Java in Eclipse. I've been able to show a ProgressDialog when the user clicks a link on a webpage in the application which is working fine.

The issue, however, is when the app is loaded for the first time and the very first webpage is loaded. The web pages take awhile and it simply shows a blank white screen whil开发者_如何学运维e the first page is loading and starting to show elements (using the Android Emulator for programming). I'd like to display the same ProgressDialog indicator during this first page load, but have been unsuccessful in my attempts.

Here is my code below.

package com.TestWebView;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.webkit.WebView;
import  android.webkit.WebViewClient;


public class TestWebView extends Activity {


    /** Called when the activity is first created. */


    @Override
public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        WebView engine = (WebView) findViewById(R.id.web_engine);

        WebViewClient myWebClient = new  WebViewClient()
        {
            ProgressDialog pd = null;

            // Override page so it's load on my view only
            @Override
            public boolean shouldOverrideUrlLoading(WebView  view, String  url)
            {
                /*view.loadUrl(url);
                return true;*/

                //start the progress dialog
                pd = ProgressDialog.show(TestWebView.this, "", "Loading...");


                 if (url.contains("maps.google.com") == true)
                 {
                    // Load new URL Don't override URL Link
                     Uri uri = Uri.parse(url);
                     Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                     startActivity(intent);
                    return true;
                 }
                 return false;
            }

            @Override
            public void onPageFinished(WebView view, String url){
                pdHandler p = new pdHandler();
                p.sendEmptyMessage(0);
            }

            class pdHandler extends Handler {
                @Override
                public void handleMessage(Message msg) {
                  if(pd != null)
                  {
                  pd.dismiss();
                  pd = null;
                  }
                }
            }

        };


        engine.getSettings().setJavaScriptEnabled(true);
        engine.getSettings().setSupportZoom(true);
        engine.getSettings().setBuiltInZoomControls(true);
        engine.setWebViewClient(myWebClient);
        engine.loadUrl("http://www.amazon.com/gp/aw/h.html");

    }


}

Any guidance is appreciated.

Thank you.


Use this code:

@Override
    public void onPageStarted(WebView view, String url, Bitmap favicon)
    {
        myLoadingDialog.show();
        super.onPageStarted(view, url, favicon);
    }


final ProgressDialog pd = ProgressDialog.show(this, "", "Loading!", true);
setContentView(R.layout.webview);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        pd.dismiss();
    }
});
mWebView.loadUrl("file:///android_asset/index.html");


Try like This:

   ProgressDialog pg;  
   @Override
   protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    engine.setWebViewClient(new MyWebViewClient());
    }

    public class MyWebViewClient extends WebViewClient {


    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        //WebView link Loader
        if(pg==null)
        {
            pg=new ProgressDialog(NowShowingActivity.this,R.style.MyTheme);
            pg.setMessage("Loading...");
            pg.setCancelable(false);

            pg.show();

        }

        webViewNowShowing.loadUrl(url);
        return true;

    }

    @Override
    public void onPageFinished(WebView view, String url) {

        super.onPageFinished(view, url);
        try {



            if (pg.isShowing()) {

                pg.dismiss();

                pg = null;

            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }

    }

    public MyWebViewClient() {
         super();

      }
        //initial WebPage loader
      public void onPageStarted(WebView webView, String url, Bitmap favicon) {

          if(pg==null&&pullToRefresh==false){

          pg =new ProgressDialog(NowShowingActivity.this,R.style.MyTheme);
          pg.setMessage("Loading..");
          pg.setCancelable(false);

          pg.show();

          }
      }
     }      
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜