0

I have a form inside a fragment where I am collecting the inputs from different fields of an address. If the mandatory fields are empty they turn red (hence the R.drawable.edittext_bg_error below) when the user attempts to submit the form. I am currently using 'if' statements but am wondering if and how to replace them with switch-case. It looks like my code can be improved, I just don't know how. Please advise.

String etAddressOne = addressForm.findViewById(R.id.et_addressOne);
        String etAddressTwo = addressForm.findViewById(R.id.et_addressTwo);

addressOne = etAddressOne.getText().toString();
                addressTwo = etAddressTwo.getText().toString();
// additional address fields code

if (addressOne.isEmpty() || city.isEmpty() || province.isEmpty() || postalCode.isEmpty() || country.isEmpty()) {
                    showMessage("Please fill out all mandatory fields");
                    // Highlight empty fields
                    if (addressOne.isEmpty()) {
                        etAddressOne.setBackgroundResource(R.drawable.edittext_bg_error);
                    } // additional if statements for remaining fields
                } else {
                    // Submit form
                }```
BVB09
  • 805
  • 9
  • 19

0 Answers0