jvpintang
jvpintang

Reputation: 51

Alert dialog builder inside the if else statement - android

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.

Upvotes: 1

Views: 1653

Answers (4)

V-rund Puro-hit
V-rund Puro-hit

Reputation: 5534

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.

Upvotes: 0

Dharmesh Gohil
Dharmesh Gohil

Reputation: 334

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'
}

Upvotes: 0

Saurabh Vardani
Saurabh Vardani

Reputation: 1861

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();
        }
    }

Upvotes: 0

JAAD
JAAD

Reputation: 12379

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();
        }
    }

Upvotes: 1

Related Questions