Progress Dialog In Webview

10,041

Solution 1

Try this,

 // Let's display the progress in the activity title bar, like the
 // browser app does.
 getWindow().requestFeature(Window.FEATURE_PROGRESS);

 webview.getSettings().setJavaScriptEnabled(true);

 final Activity activity = this;
 webview.setWebChromeClient(new WebChromeClient() {
   public void onProgressChanged(WebView view, int progress) {
     // Activities and WebViews measure progress with different scales.
     // The progress meter will automatically disappear when we reach 100%
     activity.setProgress(progress * 1000);
   }
 });
 webview.setWebViewClient(new WebViewClient() {
   public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
     Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
   }
 });

 webview.loadUrl("http://slashdot.org/");

And if you want to make your own progressDialog then

 webView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            // Start PROGRESS DIALOG
            view.loadUrl(url);
            return true;
        }

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

        //HIDE PROGRESS DIALOG LOADING IT HAS FINISHED
      }

    });

And let me know what happen..

Solution 2

check out this:

wvCouponsAndOffers.setWebViewClient(new WebViewClient() {
        ProgressDialog progressDialog = new ProgressDialog(CouponsWebViewUI.this);

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            progressDialog.setMessage(StringConstants.PROGRESS_DIALOG_MSG);
            progressDialog.setCancelable(false);
            progressDialog.setOnKeyListener(new OnKeyListener(){
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode,
                        KeyEvent event) {
                    if(keyCode == KeyEvent.KEYCODE_SEARCH) {
                        return true;
                    }
                    else
                        return false;
                }});
            progressDialog.show();
        }
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            progressDialog.setMessage(StringConstants.PROGRESS_DIALOG_MSG);
            progressDialog.setCancelable(false);
            progressDialog.setOnKeyListener(new OnKeyListener(){
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode,
                        KeyEvent event) {
                    if(keyCode == KeyEvent.KEYCODE_SEARCH) {
                        return true;
                    }
                    else
                        return false;
                }});
            progressDialog.show();
            view.loadUrl(url);
            return true;
        }

        public void onPageFinished(WebView view, String url) {
            if (progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
        }

    });
Share:
10,041
Zach
Author by

Zach

Updated on June 14, 2022

Comments

  • Zach
    Zach about 2 years

    Simply put how can I make a progress dialog show up every time a new link is clicked in my webview. I have tried many tutorials and methods but every one of them only shows the dialog once, when the app is initially loaded.

    Here is my code

     mWebView.setWebViewClient(new WebViewClient() {
    
                 @Override
                 public boolean shouldOverrideUrlLoading(WebView view, String url)
                 {
                     view.loadUrl(url);
                     return true;
                 }
                ProgressDialog dialog = ProgressDialog.show(myActivity.this, "", 
                                "Loading. Please wait...", true);
                @Override
                public void onPageFinished(WebView view, String url) {
                 dialog.dismiss();
               }
    
  • Zach
    Zach over 12 years
    I am having issues starting the progress dialog in that spot. I added the source code.. This is the only way I can get it to run but it only runs when the app is first loaded
  • user370305
    user370305 over 12 years
    @user1166556 - You are doing wrong make a ProgressDialog object in your activity's onCreate() and just display it in shouldOverrid... and dismiss on pageFinished()