Icemanind
Icemanind

Reputation: 48736

Closing an AlertDialog box

This may seem like a simple problem to solve, but I'm new to Android so please bear with me. I have the following code fragment that displays an alert box:

Builder pwBox = new AlertDialog.Builder(this);
    AlertDialog pwDialog;
    LayoutInflater mInflater = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View pwView = mInflater.inflate(R.layout.passworddialog, null);

    Button btnSetPassword = (Button) pwView
            .findViewById(R.id.btnSetPassword);

    pwBox.setView(pwView);
    pwBox.setCancelable(false);
    pwBox.setTitle("New Password");
    pwDialog = pwBox.create();

    btnSetPassword.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            //pwDialog.dismiss(); <------ Problem Line
        }
    });

    pwDialog.show();

Everything works. The problem is, I don't have access to the "pwDialog" variable, so how do I close my dialog?

Upvotes: 0

Views: 1825

Answers (2)

nicholas.hauschild
nicholas.hauschild

Reputation: 42834

It looks as though you should have access to your pwDialog variable. You may need to declare it as final though.

final AlertDialog pwDialog = pwBox.create();

Upvotes: 1

Gligor
Gligor

Reputation: 2940

You'd want something of the sort:

private static final CommandWrapper DISMISS = new CommandWrapper(Command.NO_OP);

public static AlertDialog createDeletionDialog(final Context context,
    final String message, final String positiveLabel, final Command positiveCommand) {

  AlertDialog.Builder builder = new AlertDialog.Builder(context);
  builder.setCancelable(true);
  builder.setMessage(message);

  builder.setInverseBackgroundForced(true);
  builder.setPositiveButton(positiveLabel, new CommandWrapper(positiveCommand));
  builder.setNeutralButton("Cancel", DISMISS);
  return builder.create();
}

Upvotes: 0

Related Questions