Question

I am using the progress bar for the web view.I need to stop progress bar before the page load.I press the back button but not working in between loading.

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         requestWindowFeature(Window.FEATURE_NO_TITLE);
         setContentView(R.layout.webview);
        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();

        progressBar =new ProgressDialog(this);
        progressBar.setCancelable(true);
        progressBar.show();


        mWebView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.i("TEST", "Processing webview url click...");
                view.loadUrl(url);
                return true;
            }

            public void onPageFinished(WebView view, String url) {
                Log.i("TEST", "Finished loading URL: " +url);
                if (progressBar.isShowing()) {
                    progressBar.dismiss();
                }
            }

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Log.e("TEST", "Error: " + description);
                Toast.makeText(getApplicationContext(), "Oh no! " + description, Toast.LENGTH_SHORT).show();
                alertDialog.setTitle("Error");
                alertDialog.setMessage(description);
                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        return;
                    }



                });
                alertDialog.show();
            }
        });
        mWebView.loadUrl("http://www.google.com");
}

For back button test I use this

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            Toast.makeText(getApplicationContext(),"hi", Toast.LENGTH_LONG).show();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }
Was it helpful?

Solution

Hi Please check this out

progressBar = new ProgressDialog(this);
        progressBar.setCancelable(true);
        progressBar.setOnCancelListener(new OnCancelListener() {

            public void onCancel(DialogInterface arg0) {
                 if(progressBar.isShowing())
                      progressBar.dismiss();
                 finish();
            }
        });
            progressBar.setMessage("Loading...");
        progressBar.show();

Thanks to every one

OTHER TIPS

If I were you, I would use AsyncTask to load the data of the URL, and show the ProgressDialog in between.

  • On start, spawn off a different thread which fetches the content of the URL
  • Start displaying the ProgressDialog
  • When you have the data of the URL that the thread returned, load it in the webView
  • Dismiss the ProgressDialog

For running things in different thread, check AsyncTask out.

Use Multithreading to implement progress and visual appearence using UIWorker Thread

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top