First: create AlertDialog instance so that it can access from any where in that class as follows...
private AlertDialog mNetworkConnectionDialog = null;
@Override
protected void onCreateActivity(Bundle savedInstanceState){
Second: you didn't put @Override notation before onClick method. And instead of alertDialog.show(); you should pass your alertDialog to mNetworkConnectionDialog by creating as follows...
mNetworkConnectionDialog = alertDialog.create();
Instead of passing InterPreterWithAnimation.this into AlertDialog.Builder constructor you can also pass only this. Here, I'm giving the corrected version of code...
if (!haveNetworkConnection) {
// Toast.makeText(getApplicationContext(), "No Connection",
// Toast.LENGTH_SHORT).show();
advtlayout.setVisibility(View.GONE);
System.out.println("no network");
showGraphOverlay();
hideOutputTextView();
hideProgressBar();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
InterPreterWithAnimation.this);
// Setting Dialog Title
alertDialog.setTitle("No Network");
// Setting Dialog Message
alertDialog.setMessage("Turn on Wifi or Mobile data");
// Setting Icon to Dialog
// alertDialog.setIcon(R.drawable.);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke YES event
InterPreterWithAnimation.this.startActivity(new Intent(
Settings.ACTION_SETTINGS));
dialog.dismiss();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
dialog.dismiss();
}
});
// Showing Alert Message
mNetworkConnectionDialog = alertDialog.create();
mNetworkConnectionDialog.show();
}
Third: Since you want dismiss your dialog when onPause method called then you should write mNetworkConnectionDialog.dismiss(); as follows...
@Override
protected void onPause() {
if (mNetworkConnectionDialog != null) {
mNetworkConnectionDialog.dismiss();
}
}