0

My need is the following: in a form, I have 7 fields and each one can be in error. When I lose focus on a field, I have to check if the data is valid and if not, change the TintColor for red.

The problem I got is the following: impossible to set the color to red.

After many and many tries, I finally realized why: my color change is override by the natural behaviour. What I really need is not "onFocusChange", but something like "AfterFocusChange" to be sure I have the last word about that color.

Sadly, it seems it doesn't exists. Do you have any idea how I can manage to do it?

Currently, my code looks like this:

@OnFocusChange(R.id.create_account_password)
public void onPasswordFocusLost(boolean focused) {
    if (!focused) {
        Matcher matcher = VALID_PASSWORD_REGEX.matcher(passwordField.getText());
        isPasswordInError = !matcher.matches() || passwordField.getText().length() >= 8;
    }
    updateErrorDisplay(isPasswordInError, passwordField, passwordError, passwordRule);
}

and

private void updateErrorDisplay(boolean showError, EditText field, TextView error, TextView rule) {
    if (showError) {
        error.setVisibility(View.VISIBLE);
        field.getBackground().setColorFilter(ContextCompat.getColor(this, R.color.red),
                PorterDuff.Mode.SRC_ATOP);
        if (rule != null) {
            rule.setVisibility(View.VISIBLE);
        }
    } else {
        error.setVisibility(View.GONE);
        field.getBackground().clearColorFilter();
        if (rule != null) {
            rule.setVisibility(View.GONE);
        }
    }
}

Thanks!

1 Answers1

0

You can use TextInputLayout with AppCompatEditView, You can validate the fields and display the error message. Color of underline can be set based on the validation.

  • Yeah, that's basically what I have. The only problem is the behaviour of the color is not exactly what I need, because the value is override by the default behaviour. What I mean is that the original behaviour already changes the color, so it does override what I just set. – Q Anacho D Mar 16 '17 at 12:40
  • http://stackoverflow.com/questions/26574328/changing-edittext-bottom-line-color-with-appcompat-v7 – vikash kumar pandey Mar 17 '17 at 13:09
  • I need to change it dynamically and not for all fields, so it doesn't apply – Q Anacho D Mar 17 '17 at 14:46