0


I'm building an app with many Edittexts in it and I end up repeating the same code over and over..
For each Edditext I hide the hint and set a label above it, I set the same errors to all of them and that seems to me like a waste..

Is there anyway I could write that code ones and apply it to all Edittexts?

for example: edittext.setAllErrors(); or any other way that maybe i am not familiar with?

Example for a code that I repeat for almost all Edittext:

        firstNameET.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {

            if(hasFocus || isFull(firstNameET.getText().toString()) == false) {
                firstNameET.setHint("");
                firstNameLabel.setVisibility(TextView.VISIBLE);


                //letter checker
                if(charCheck(firstNameET.getText().toString().trim()) == false && !firstNameET.getText().toString().trim().matches("")){
                    firstNameET.setError("Only letters are allowed");
                }
                if(lengthCheck(firstNameET.getText().toString().trim()) == 0 && focusCount > 1){
                    firstNameET.setError("Please Insert Name");
                }else if(lengthCheck(firstNameET.getText().toString().trim()) == 2){
                    firstNameET.setError("Name is not long enough.");
                }

            }
            else{
                firstNameET.setHint("First Name");
                {
                    firstNameLabel.setVisibility(TextView.INVISIBLE);
                }
            }
        }
    });
Shay Mishali
  • 97
  • 1
  • 6

0 Answers0