0

I wanted to make an Alert Dialog to ask some informations from the user before further actions, and so I made the Alert Dialog with two EditTexts to be filled. However, I couldn't find how to make it so that the dialog doesn't close if the user clicks on the Negative Button while the EditTexts are not completed. I tried with a while loop but my app freezes when doing this so i guess that wasn't a viable solution.

The code for my Alert Dialog looks like this :

public class CommandPopUp extends AppCompatDialogFragment {
    private EditText editTextPhoneNumber;
    private EditText editTextAddress;
    private TextView title;

    public CommandPopUp(Context context) {
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.pop_up_command,null);

        builder.setView(view)
                .setNegativeButton("Valider la commande", (dialog, which) -> {

                    String phoneNumber = editTextPhoneNumber.getText().toString();
                    String address = editTextAddress.getText().toString();


                    if(phoneNumber.length()!=10){
                        editTextPhoneNumber.setError("Veuillez entrer un numéro valide");
                    }

                    if(address.isEmpty()){
                        editTextAddress.setError("Veuillez entrer votre adresse");
                    }

                    //Don't do it till both above conditions are unverified

                    CartModel cart = null; //=....
                    deleteCart();
                    createCommand(cart, phoneNumber, address);

                    Toast.makeText(getContext(), "Votre commande a bien été prise en compte", Toast.LENGTH_SHORT).show();
                })
                .setPositiveButton("Annuler", (dialog, which) -> dialog.dismiss());

        editTextPhoneNumber = view.findViewById(R.id.editTextTel);
        editTextAddress = view.findViewById(R.id.editTextAddress);
        title = view.findViewById(R.id.titlePopUp);
        title.setText(new StringBuilder("Votre commande"));
        
        return builder.create();
    }

    private void deleteCart() {
        FirebaseDatabase.getInstance()
                .getReference("Cart")
                .child("UNIQUE_USER_ID")
                .removeValue();
    }

    private void createCommand(CartModel cart, String address, String phoneNumber) {
    }
}

Thanks in advance

Balizok
  • 904
  • 2
  • 5
  • 19

1 Answers1

0

The code is not very clear, but if I got it, you just need an if-else statement.

First check the first condition:

if(phoneNumber.length()!=10){
           editTextPhoneNumber.setError("Veuillez entrer un numéro valide");
}

Then the second condition:

if(address.isEmpty()){
           editTextAddress.setError("Veuillez entrer votre adresse");
}

Then you need an ELSE condition, so it will be executed only if both conditions are false.

CartModel cart = null; //=....
deleteCart();
createCommand(cart, phoneNumber, address);
Toast.makeText(getContext(), "Votre commande a bien été prise en compte", Toast.LENGTH_SHORT).show();

Also, I don't remember if it works with AlertDialog, but you can simply add a return statement in if conditions in order to stop the checks. Like this:

if(phoneNumber.length()!=10){
          editTextPhoneNumber.setError("Veuillez entrer un numéro valide");
          return;
}
Stefano Leone
  • 635
  • 5
  • 14
  • I tried it but even if the code to delete the cart and create the command doesn't run, my Alert Dialog closes and nothing happens. My goal is to run this code only after the conditions are verified, and trying to press the button while they are not just shows the errors on the editTexts and doesn't close the Dialog. – Balizok Jun 05 '21 at 23:15
  • What do you mean? – Stefano Leone Jun 05 '21 at 23:19
  • What i want is, when pressing the button, not to do anything if the address and phoneNumber editTexts are not completed and just display the errors. However, when arriving at the end of the OnClickListener of this button, the Dialog dismisses, so I wondered how to make it so that it loops till the address and phone number are provided, and then runs the code (or if there's a way not to dismiss the Dialog at the end of the OnClickListener). – Balizok Jun 05 '21 at 23:29
  • Does it close your Dialog when it goes inside an if statement? – Stefano Leone Jun 06 '21 at 00:32
  • Just found out that my question was a duplicate of [How to prevent a dialog from closing when a button is clicked](https://stackoverflow.com/questions/2620444/how-to-prevent-a-dialog-from-closing-when-a-button-is-clicked/10661281#10661281) so i found there a solution to my problem, thank you :) – Balizok Jun 06 '21 at 11:13