Search code examples
androidandroid-activitywebviewloadingprogress

How to display a progress bar with webview


This is my last code and. I want to show user network info. When user is offline alert to the user.
And I want to show to user in offline or online. I use progress bar. When I use this code my Application is crashing and go out of my application. What should I do

   ConnectivityManager conMgr;
   NetworkInfo netInfo;
    private WebView webView;
    ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_activity1);

           Toast.makeText(Activity5.this, "welcome",30000).show();

            progressDialog = new ProgressDialog(Activity5.this);
            Button btn1 = (Button) findViewById(R.id.buttn1);
            btn1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    finish();
                    System.exit(0);
                }
            });
            Button b = (Button) findViewById(R.id.button10);
            b.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    startActivity(new Intent(Activity5.this, Activity2.class));
                }
            });


            String url = "http://student.iaun.ac.ir";
            webView = (WebView) findViewById(R.id.webView2);

            webView.setWebViewClient(new myWebViewClient());
            webView.getSettings().setJavaScriptEnabled(false);
            webView.getSettings().setLoadWithOverviewMode(true);
            webView.getSettings().setUseWideViewPort(true);
            webView.getSettings().setBuiltInZoomControls(true);
            webView.loadUrl(url);
            webView.getSettings().setSavePassword(false);
            Button button = (Button) findViewById(R.id.new_button);
            button.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                        startActivity(new Intent(Activity5.this, Prefs.class));
                }
        });


            progressDialog.setMessage("please wait web page is loading");
            progressDialog.setCancelable(false);
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.show();

            new Handler().postDelayed(new Runnable() {
                public void run() {

                    progressDialog.dismiss();

                }
            }, 25000);

            progressDialog = new ProgressDialog(Activity5.this);
                if (isOnline()) {
                       /*Your Code*/
                  }
                else{
                   try {
                AlertDialog alertDialog = new AlertDialog.Builder(
                        Activity5.this).create();
                alertDialog.setTitle("Info");
                alertDialog.setMessage("Please check your internet connection");
                alertDialog.setButton("OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                finish();
                            }
                        });
                alertDialog.show();
            } catch (Exception e) {
            }
        }


            new Thread(new Runnable() {
                @Override
                public void run() {
                                  try {
                                        startDelay();
                                        webView.getSettings().setJavaScriptEnabled(true);     
                                  } catch (InterruptedException e) {
                                     e.printStackTrace();
                                    }

                        }
            }).start();    

    }



    void startDelay() throws InterruptedException {
        Thread.sleep(15000);
    }
       private boolean isOnline() {
            conMgr = (ConnectivityManager) getApplicationContext()
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            netInfo = conMgr.getActiveNetworkInfo();

            if (netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()) {
                return false;
            }
            return true;
        }

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity1, menu);
        return true;


    }


    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button and if there's history
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        // If it wasn't the Back key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
        return super.onKeyDown(keyCode, event);
    }

Solution

  • Points are :

    1. You can't use System.exit(0) on button click, you can use finish() to complete that activity or moveTaskToBack(true) to minimize the application.

    2. I would like to refer u this link :

    3. Firstly understand every thing, then move forward, not just copy & paste thee code, Understand the concept and logic behind it.

    4. If you need more help on this topic then I am here, but take your time and try to understand the logics