Search code examples
javaandroidandroid-studiomaterial-uiandroid-alertdialog

Android Material UI dialog, keep the dialog open when the button is pressed


I want my dialog to stay open when I press the OK button, because I want to make the Alert Dialog the place to input the verification code. Is there a way to keep the Alert Dialog from closing?


                                MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(getContext())
                                        .setTitle("Verifikasi email")
                                        .setIcon(R.drawable.icons8_checkmark)
                                        .setMessage("message")
                                        .setView(viewDig)
                                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                // Aksi yang akan dilakukan ketika tombol "OK" diklik
                                                dialog.dismiss();

                                                StringBuilder sb = new StringBuilder();
                                                for (EditText editText : editTexts) {
                                                    sb.append(editText.getText().toString());
                                                }
                                                String verNum = sb.toString();

                                                if (verNum.equals(activation)){
                                                    // do something without closing the dialog
                                                }else{
                                                    // do something without closing the dialog
                                                }
                                            }
                                        });
                                builder.setCancelable(false);
                                builder.show();

Solution

  • nvm, the Alert dialog is working. I just need to make the PostiveButton null and make the onClick listener separate from the PositiveButton , here is the code

    MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(getContext())
            .setTitle("Verifikasi email")
            .setIcon(R.drawable.icons8_checkmark)
            .setMessage("message")
            .setView(viewDig)
            .setPositiveButton("OK", null);
    
    AlertDialog dialog = builder.create();
    dialog.setCancelable(false);
    
    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialogInterface) {
            Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
            positiveButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // Do something when the "OK" button is clicked
                    // Add your desired functionality here
    
                    // To keep the dialog open, do not call dialog.dismiss()
                }
            });
        }
    });
    
    dialog.show();