Stefano
Stefano

Reputation: 199

Inception of AlertDialog

Is it possible (and correct) to make an inception of AlertDialog? I mean an AlertDialog where .setPositiveButton call another AlertDialog.

If yes, can the two AlertDialog have the same name?

Example:

final AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);

builder
        .setMessage("Do you want to register a new account?")
        .setPositiveButton("Register", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                AlertDialog.Builder builder2 = new AlertDialog.Builder(LoginActivity.this);
                builder2
                        .setView(LoginActivity.this.getLayoutInflater().inflate(R.layout.dialog_register, null))
                        .setPositiveButton("E", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Log.i("Stefano", "2");
                            }
                        })
                        .setNegativeButton("w", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Log.i("Stefano", "3");
                            }
                        })
                        .create()
                        .show();
        })
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                return;
            }
        })
        .create()
        .show();

Upvotes: 0

Views: 69

Answers (1)

ישו אוהב אותך
ישו אוהב אותך

Reputation: 29844

Is it possible (and correct) to make an inception of AlertDialog?

Yes, it is possible.

If yes, can the two AlertDialog have the same name?

You can have two AlertDialog with the same variable name similarly like using two variables with the same name. It is called Shadowing:

If a declaration of a type (such as a member variable or a parameter name) in a particular scope (such as an inner class or a method definition) has the same name as another declaration in the enclosing scope, then the declaration shadows the declaration of the enclosing scope. You cannot refer to a shadowed declaration by its name alone.

In your case, you're creating another dialog inside anonymous class which is a DialogInterface.OnClickListener. The shadowing still apply. The Anonymous Classes documentation describe it:

Like local classes, anonymous classes can capture variables; they have the same access to local variables of the enclosing scope:

An anonymous class has access to the members of its enclosing class.

An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively final.

Like a nested class, a declaration of a type (such as a variable) in an anonymous class shadows any other declarations in the enclosing scope that have the same name. See Shadowing for more information.

So, you can change your code to something like this:

final AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);

builder.setMessage("Do you want to register a new account?")
    .setPositiveButton("register", new DialogInterface.OnClickListener() {
      @Override public void onClick(DialogInterface dialog, int which) {
        AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
        builder.setView(getLayoutInflater().inflate(R.layout.dialog_register, null))
            .setPositiveButton("E", new DialogInterface.OnClickListener() {
              @Override public void onClick(DialogInterface dialogInterface, int i) {
                Log.i("Stefano", "2");
              }
            })
            .setNegativeButton("w", new DialogInterface.OnClickListener() {
              @Override public void onClick(DialogInterface dialogInterface, int i) {
                Log.i("Stefano", "3");
              }
            })
            .create()
            .show();
      }
    })
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int id) {

      }
    })
    .create()
    .show();

But you better to split the dialogs to 2 different parts. Why? Because it's easier to see and minimize your time to read and comprehend the code. Always write code that humans can understanding. So, split up the code to something like this:

  private void showDialog() {
    final AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);

    builder.setMessage("Do you want to register a new account?")
        .setPositiveButton("register", new DialogInterface.OnClickListener() {
          @Override public void onClick(DialogInterface dialog, int which) {
            // show another dialog when clicked.
            showAnotherDialog();
          }
        })
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {

          }
        })
        .create()
        .show();
  }

  // This is the code for dialog inside your first dialog.
  private void showAnotherDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
    builder.setView(getLayoutInflater().inflate(R.layout.dialog_register, null))
        .setPositiveButton("E", new DialogInterface.OnClickListener() {
          @Override public void onClick(DialogInterface dialogInterface, int i) {
            Log.i("Stefano", "2");
          }
        })
        .setNegativeButton("w", new DialogInterface.OnClickListener() {
          @Override public void onClick(DialogInterface dialogInterface, int i) {
            Log.i("Stefano", "3");
          }
        })
        .create()
        .show();
  }

Upvotes: 1

Related Questions