1

I put Alert dialog builder inside my if else statement. Now, I want to do is to automatic dismiss the dialog in "if" statement and show the dialog in "else" statement. So, here is my codes.

 public void DisplayConn(){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Error");
    builder.setMessage("No Network Connection").setCancelable(false);
    AlertDialog alert = builder.create();

    if(isNetworkStatusAvailable(getApplicationContext())){
        alert.dismiss();
    } else {
        alert.show();
    }
}

I don't want to use onClick button to dismiss the dialog, I just want to automatic dismiss the dialog in "if" statement. It is possible? Thanks in advance.

jvpintang
  • 51
  • 8
  • public void DisplayConn(){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Error"); builder.setMessage("No Network Connection").setCancelable(false); AlertDialog alert = builder.create(); if(isNetworkStatusAvailable(getApplicationContext())){ } else { alert.show(); } } try this – saeed Apr 15 '16 at 09:50
  • yes it is possible just don't call show if condition is true – JAAD Apr 15 '16 at 09:50
  • and remove dimiss call – JAAD Apr 15 '16 at 09:51
  • Unnecessarily you are calling alert.dismiss(); in if part even though you have not actually shown the alert. You have just created it and not shown. Please remove that. Rest will work. In any case if you want to dismiss your alert dialog, please put a check like if(alert!=null && alert.isShowing){ alert.dismiss(); } – Ankita Apr 15 '16 at 09:54
  • do you want your `Dialog` to be automatically destroyed when Internet is available? – V-rund Puro-hit Apr 15 '16 at 09:54
  • @vrundpurohit Yes, i want dialog automatically destroyed when Internet is connected. – jvpintang Apr 15 '16 at 16:05
  • @saeed that is only show the dialog, i need is to show the dialog when the internet connection is lost and destroy the dialog automatic when the internet is connected again. – jvpintang Apr 15 '16 at 16:11
  • Ok now iam out of the desk be cool i will help you – saeed Apr 15 '16 at 17:23
  • @saeed okay. thaaanks. – jvpintang Apr 15 '16 at 17:28
  • try with Broadcast receiver http://stackoverflow.com/a/26114247/3790150 – saeed Apr 16 '16 at 03:16

4 Answers4

1

Change your code to this:

public void DisplayConn(){
        if(!isNetworkStatusAvailable(getApplicationContext())){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Error");
        builder.setMessage("No Network Connection").setCancelable(false);
        AlertDialog alert = builder.create();
            alert.show();
        }
    }
JAAD
  • 12,349
  • 7
  • 36
  • 57
  • ok, no issues let's put this topic to rest, please don't edit other's answer to make it same as other's answer already posted – JAAD Apr 15 '16 at 10:12
  • The dialog is not destroy when I connect it again in the internet. – jvpintang Apr 15 '16 at 16:07
0

You can do like this...

   public void DisplayConn(){

        if(!isNetworkStatusAvailable(getApplicationContext())){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Error");
        builder.setMessage("No Network Connection").setCancelable(false);
        AlertDialog alert = builder.create();
            alert.show();
        }
    }
saeed
  • 1,935
  • 1
  • 18
  • 33
Saurabh Vardani
  • 1,821
  • 2
  • 18
  • 33
  • how it is different from my answer – JAAD Apr 15 '16 at 10:01
  • @ankitagrawal..Dear I upvoted your answer... I think there is hardly 10 seconds difference in between yours and mine answer...I wrote the same answer from different way but Mr. Saeed changed my answer as you can see.... – Saurabh Vardani Apr 15 '16 at 10:03
  • i was asking @saeed only , not you as i have already seen he edited your answer – JAAD Apr 15 '16 at 10:04
  • 1
    @ankitagrawal If you are asking to saeed then please first of all mention his name....as i am seeing your profile you are a great person.... don;t do these foolish things... we are here to help someone not for gaining the points.... – Saurabh Vardani Apr 15 '16 at 10:06
  • dear since i posted after his edit , I think it was obvious who i was asking , since your posted answer was different , it was your misunderstanding not my foolishness – JAAD Apr 15 '16 at 10:10
  • The dialog is not gone when I connect it again in the internet. – jvpintang Apr 15 '16 at 14:40
0

You can try following

public void DisplayConn(){
    if(isNetworkStatusAvailable(getApplicationContext())){
        Pop.on(this).with().title("Error").cancelable(false).body("No Network Connection").show();
    }
}

after including this lib in your gradle dependencies

dependencies {
    compile 'com.vistrav:pop:2.0'
}
Dharmesh Gohil
  • 324
  • 2
  • 6
0

I think BroadcastReciver will do the trick.

try something like this.

if(isNetworkStatusAvailable(getApplicationContext())){
    // do your work
} else {
    this.registerReceiver(mConnReceiver, new IntentFilter(
                    ConnectivityManager.CONNECTIVITY_ACTION));
    alert.show();
}

And your BroadcastReciver would be as below.

private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {

        if (isNetworkStatusAvailable(getApplicationContext())) {
            alert.dismiss();
        } else {
            // do nothing 
            // Toast.makeText(getApplicationContext(), "Not Connected", 500)
            //      .show();
        }
    }
};

and final task.. you have to register this BroadcastReciver in onResume and unregister in onPause.

@Override
protected void onPause() {
    super.onPause();
    Your_class.this.unregisterReceiver(mConnReceiver);
}

protected void onResume() {
    super.onResume();
    IntentFilter i = new IntentFilter(
            "packagename.classname");
    Your_class.this.registerReceiver(mConnReceiver, i);
}

I hope this will help you out.

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50