title: Popup dialog when no data connection present - Android
body: <p>I am trying to get an alert dialog to appear when trying to load a webview when no internet connection is present. I've placed this class below the OnCreate but I am having no luck getting the dialog to appear. All suggestions would be appreciated. Thank you!</p>
  
  <pre><code>public void open(View view) {
      AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
      alertDialogBuilder.setMessage("No data connection available");
      alertDialogBuilder.setPositiveButton("OK",
              new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface arg0, int arg1) {
                      Toast.makeText(RepActivity.this, "Check connection settings", Toast.LENGTH_LONG).show();
                  }
              });
  
      alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
              finish();
          }
  
      });
      ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
      android.net.NetworkInfo wifi = cm
              .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
      android.net.NetworkInfo datac = cm
              .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
      if ((wifi != null &amp; datac != null)
              &amp;&amp; (wifi.isConnected() | datac.isConnected())) {
          setContentView(R.layout.activity_rep);
      } else {
          //no connection
          AlertDialog.Builder alertDialog = new AlertDialog.Builder(RepActivity.this);
          alertDialog.show();
      }
  }
  </code></pre>