Android - ViewCompat setBackgroundTintList not working in API 21

I have an AppCompatEditText with the backgroundTint property set to a specific color. I created a method for changing the background color programmatically and working in all versions of Android, from API 17 (4.2 Jelly Bean) to API 25 (7.1.1 nougat), except for API 21 (5.0 Lollipop) .

I do not know what I am doing wrong. Here is my code:

    public void changeViewBackgroundColor(Context context, View view, int color) {
      int theColor = ContextCompat.getColor(context, color);

      if (view instanceof TintableBackgroundView) {
        ColorStateList colorStateList = ColorStateList.valueOf(theColor);
        ViewCompat.setBackgroundTintList(view, colorStateList);
      } else {
        view.setBackgroundColor(theColor);
      }

      view.invalidate();
  }
+6
source share

All Articles