user2303640
user2303640

Reputation: 1

Dialog Box Close Automatically

I would like to add dialog box in my coding. The dialog box is able to pop out. Suppose the dialog will close after user press the ok button, but the dialog box close automatically. This is my code. Any problem?

private void updataAccount(int type){
    Iterator<AccountData> iteratorSort = commondata.account.values().iterator();
    while (iteratorSort.hasNext()){
        AccountData data = iteratorSort.next();
        if(data.id == Integer.parseInt(accountId[account_spn.getSelectedItemPosition()]))
        {
            if(type == INCOME_MODE){
                data.balance = data.balance+Double.parseDouble(value);
                commondata.updateAccount(data);
            }else if(type == PAYOUT_MODE){
                data.balance = data.balance-Double.parseDouble(value);
                commondata.updateAccount(data);
                if(data.balance < 0)
                {
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder
                    .setTitle("Care Money")
                    .setMessage("Your amount in this account is negative!")
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() 
                    {
                        public void onClick(DialogInterface dialog, int which) 
                        {       
                               dialog.dismiss();
                    }
                    });                     
                AlertDialog alert = builder.create();
                        alert.show();
                }
            }
            return;
        }
    }

Upvotes: 0

Views: 1727

Answers (3)

LuckyMe
LuckyMe

Reputation: 3910

Create an AsyncTask that displays the dialog, then sleeps for 1000 milliseconds, then closes the dialog.

Upvotes: 0

Developer
Developer

Reputation: 6350

Chitan Please try this code It is working fine for me hope it will help u somehow

AlertDialog.Builder builder = new AlertDialog.Builder(this);
                     builder.setTitle("Error");
                     builder.setMessage(error)
                            .setCancelable(false)
                            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    error="";
                                }
                            });
                     AlertDialog alert = builder.create();
                     alert.show();

Upvotes: 0

NewAndroidTechie
NewAndroidTechie

Reputation: 37

Try using this

builder.setCancelable(false);
    builder.show();

Instead of this

AlertDialog alert = builder.create();
            alert.show();

Upvotes: 1

Related Questions