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