0

Here I have to show two AlertDialogs. Second AlertDialog will be shown just after click on positive button of first AlertDialog.

MainActivity.class

public class MainActivity extends AppCompatActivity {

public static final String ACTION_NEXT = "Next";
public static final String ACTION_DONE = "Done";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setupSecretFields(this, "Configuration", "Please Enter Password", ACTION_NEXT);
}

private void setupSecretFields(final Context context, final String title, final String hint, final String button) {
    final AppCompatEditText appCompatEditText = new AppCompatEditText(context);
    appCompatEditText.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    appCompatEditText.setHint(hint);
    appCompatEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

    final LinearLayout linearLayout = new LinearLayout(context);
    linearLayout.addView(appCompatEditText);
    linearLayout.setPadding(40, 30, 40, 20);
    linearLayout.setLayoutParams(new WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT));

    final AlertDialog builder = new AlertDialog.Builder(context, R.style.CustomAlert)
            .setPositiveButton(button, null)
            .setNegativeButton("Cancel", null)
            .setView(linearLayout)
            .setCancelable(false)
            .create();
    builder.show();

    builder.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (button.equals(ACTION_NEXT)) {
                if (appCompatEditText.getText().toString().trim().equals("1234")) {
                    builder.dismiss();
                    //Showing another AlertDialog
                    setupSecretFields(context, "Configuration", "Please Enter Api Url", ACTION_DONE);
                } else {
                    appCompatEditText.setError("Incorrect Password! Please try again");
                }
            } else {
                Toast.makeText(context, "Application Will Restart...", Toast.LENGTH_LONG).show();
            }
        }
    });
  }
}

First Dialog is working fine, But it is not showing another AlertDialog. Don't know what's wrong with it. Please Help.

User
  • 4,023
  • 4
  • 37
  • 63

1 Answers1

0

Change:

final AlertDialog builder = new AlertDialog.Builder(context, R.style.CustomAlert)
        .setPositiveButton(button, null)
        .setNegativeButton("Cancel", null)
        .setView(linearLayout)
        .setCancelable(false)
        .create();
builder.show();

to:

final AlertDialog builder = new AlertDialog.Builder(context, R.style.CustomAlert)
        .setPositiveButton("Button", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
            //DO YOUR TASK (open your second dialog)
            }
        })
        .setNegativeButton("Cancel", null)
        .setView(linearLayout)
        .setCancelable(false)
        .create();
builder.show();

Should help u out.

Strider
  • 4,452
  • 3
  • 24
  • 35
  • Thankyou sir but this OnClickListener will close the dialog after hit. I dont want to close it if user entered wrong password. see @eric 's answer here.. http://stackoverflow.com/questions/2620444/how-to-prevent-a-dialog-from-closing-when-a-button-is-clicked – User Feb 15 '16 at 11:59