Neo
Neo

Reputation: 16239

Need a loader everytime when click on new link on webview in android app

I want a loader to be displayed every time when I clicked on any link inside webview.

currently at application start only it is showing me loader.

used following code

some time for every load loader is coming sometimes not , not sure why it is happening.

// Force links and redirects to open in the WebView instead of in a browser
    mWebView.setWebViewClient(new WebViewClient() {      
            ProgressDialog progressDialog = null;

        //If you will not use this method url links are opeen in new brower not in webview
        public boolean shouldOverrideUrlLoading(WebView view, String url) {    
     if (progressDialog == null) {
                // in standard case YourActivity.this
                progressDialog = new ProgressDialog(MainActivity.this);
                progressDialog.setMessage("Loading...");
                progressDialog.show();
            }          
            view.loadUrl(url);
            return true;
        }

        //Show loader on url load
        public void onLoadResource (WebView view, String url) {
            if (progressDialog == null) {
                // in standard case YourActivity.this
                progressDialog = new ProgressDialog(MainActivity.this);
                progressDialog.setMessage("Loading...");
                progressDialog.show();
            }
        }


        public void onPageFinished(WebView view, String url) {
            try{
            if (progressDialog.isShowing()) {
                progressDialog.dismiss();
                progressDialog = null;
            }   
            }
            catch(Exception exception){
                exception.printStackTrace();
            }
        }

        //Show error page
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            mWebView.loadUrl("file:///android_asset/error.html");
        }             
    }); 

     // Javascript enabled on webview  
    mWebView.getSettings().setJavaScriptEnabled(true);         



    //Load url in webview
    mWebView.loadUrl(url);

}

Upvotes: 1

Views: 2325

Answers (2)

Md. Ilyas Hasan Mamun
Md. Ilyas Hasan Mamun

Reputation: 1888

Try This:

   @Override
   public boolean shouldOverrideUrlLoading(WebView view, String url) {    
   if (progressDialog == null) {
            // in standard case YourActivity.this
            progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.setMessage("Loading...");
            progressDialog.show();
        }          
        view.loadUrl(url);
        return true;
    }

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

        super.onPageFinished(view, url);
        try {

            if (progressDialog .isShowing()) {

                progressDialog .dismiss();

                progressDialog = null;

            }

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

    }

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

 }

 public void onPageStarted(WebView webView, String url, Bitmap favicon) {

        super.onPageStarted(webView, url, favicon);

        if (progressDialog == null) {
            // in standard case YourActivity.this
            progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.setMessage("Loading...");
            progressDialog.show();
        }            

 }

Upvotes: 5

ksasq
ksasq

Reputation: 4412

There are a couple of issues I can spot in your code.

  1. Please do not call WebView.loadUrl from shouldOverrideUrlLoading. It's not needed, and in fact creates a loop. The load is already in progress.

  2. Please show the dialog from the onPageStarted callback, and dismiss it from onPageFinished (like you are now). onLoadResource and onShouldOverrideUrlLoading aren't the right places to display it for the effect you are looking for.

Hope this helps!

Upvotes: 1

Related Questions