Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I Want to timeout my webview if it takes a long time to load showing an error message. I'm using setWebViewClient because I need to use the public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) .

I've been looking around and saw I can use the method onProgressChanged(WebView view, int newProgress) . Now I can't use this method in setWebViewClient and can't figure out how to go about solving this problem. Another issue I have is that the progress bar never goes away once the page is loaded, I can't add a breakpoint to the method public void onPageFinished(WebView view, String url) either.

Web View Settings Method:

public void WebViewSettings(){
    webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.canGoBack();
    webView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals(urlString)) {
                // This is my web site, so do not override; let my WebView load the page
                return false;
            // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        @Override
        public void onLoadResource(WebView view, String url) {
            // Check to see if there is a progress dialog
            if (progressDialog == null) {
                progressDialog = new ProgressDialog(context);
                progressDialog.setTitle("Loading...");
                progressDialog.setMessage("Please wait.");
                //progressDialog.setCancelable(false);
                progressDialog.setIndeterminate(true);
                progressDialog.show();
        @Override
        public void onPageFinished(WebView view, String url) {
            // Page is done loading;
            // hide the progress dialog and show the webview
            if (progressDialog.isShowing()) {
                progressDialog.dismiss();
                progressDialog = null;
                webView.setEnabled(true);
        @Override
        public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
             handler.proceed();
        @Override
        public void onReceivedError(WebView view, int errorCod,String description, String failingUrl) {
            Toast.makeText(context, "Your Internet Connection May not be active Or " + description , Toast.LENGTH_LONG).show();

So the issues I have are the progress bar doesn't get removed once the web page has loaded and I need to timeout the webview if it take over a certain amount of time to load. It looks like the progress bar is shown, then is disappears like it should, then starts loading again and wont stop. Thanks

I have updated your webview setting method please find below updated code. I have added one loaderTimerTask class. Added code for timer and put comment for more understanding. Please update this code if you need.

private boolean isPageLoadedComplete = false; //declare at class level
public void WebViewSettings(){
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.canGoBack();
  *I had put code here
Timer myTimer = new Timer();
    //Start this timer when you create you task
myTimer.schedule(loaderTask, 3000); // 3000 is delay in millies
webView.setWebViewClient(new WebViewClient(){
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals(urlString)) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    @Override
    public void onLoadResource(WebView view, String url) {
        // Check to see if there is a progress dialog
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(context);
            progressDialog.setTitle("Loading...");
            progressDialog.setMessage("Please wait.");
            //progressDialog.setCancelable(false);
            progressDialog.setIndeterminate(true);
            progressDialog.show();
    @Override
    public void onPageFinished(WebView view, String url) {
            isPageLoadedComplete = true;
        // Page is done loading;
        // hide the progress dialog and show the webview
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
            progressDialog = null;
            webView.setEnabled(true);
    @Override
    public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
         handler.proceed();
    @Override
    public void onReceivedError(WebView view, int errorCod,String description, String failingUrl) {
        Toast.makeText(context, "Your Internet Connection May not be active Or " + description , Toast.LENGTH_LONG).show();
  *This class is invoke when you times up.
 class loaderTask extends TimerTask {
  public void run() {
    System.out.println("Times Up");
    if(isPageLoadedComplete){
    }else{
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
            progressDialog = null;
            webView.setEnabled(true);
        //show error message as per you need.
                thanks for that mate. The only thing with it is when I add an error message where you've put //show error message as per you need., I get a runtime error saying Can't create handler inside thread that has not called looper.prepare(). now I've looked around and it's apparently something to do with the thread it's on, can you suggest anything?
– Paddy1990
                Mar 31, 2014 at 14:21
                I've managed to fix it I had to explicitly tell it to run the error message in runOnUiThread(new Runnable(){public void run(){   Toast.makeText(context, "The web page timed out, try again", Toast.LENGTH_SHORT).show();}});
– Paddy1990
                Mar 31, 2014 at 14:37
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.