Saturday, 10 December 2022

Changing Android icon color with PorterDuff.Mode.SRC_ATOP usually works, but not always

Scenario

In testing, and in most cases, the app responds to setColorFilter as expected (the icon changes color). I've recently had a report that a user with Samsung S22+ is NOT seeing the icons change color.

Relevant Code

I've pulled out what I thought to be code that is applicable from the overall application.

int colorIconActive = ContextCompat.getColor(context, R.color.colorIconActiveDark); //<color name="colorIconActiveDark">#3199BB</color>
int colorIconNonActive = ContextCompat.getColor(context, R.color.colorIconNonActiveDark); //<color name="colorIconNonActiveDark">#444444</color>

Drawable tapIcon = ContextCompat.getDrawable(this, R.drawable.tap); // tap.png
View tapIcon = findViewById(R.id.tap_icon); 
/*
       <ImageView
            android:id="@+id/tap_icon"
            android:onClick="onClickUpdateIcons"
            android:layout_width="0dp"
            android:layout_weight="20"
            android:layout_height="match_parent"
            android:src="@drawable/tap"
            android:contentDescription="draught beer"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            />
*/

if (condition) {
    tapIcon.setColorFilter(colorIconNonActive, PorterDuff.Mode.SRC_ATOP);
else {
    tapIcon.setColorFilter(colorIconActive, PorterDuff.Mode.SRC_ATOP);
}

tapIcon.invalidate();

This code has been working on all devices, as far as I know, for years. I just got a report today that indicates the icon colors are not changing. The underlying function of touching the icon DOES work for this user, but the icons are reported to not be changing color.

  • compileSdkVersion 30
  • targetSdkVersion 30
  • minSdkVersion 14

Question

Are there any known problems when using the icon color change technique that's being employed above? If so, do those problems have work-arounds? I wrote this a long time ago, and so don't know what's going on with invalidate(), but it's been working up until now. Is there a more reliable way to change icon color, hopefully while not abandoning people who are limping along with an old phone?



from Changing Android icon color with PorterDuff.Mode.SRC_ATOP usually works, but not always

No comments:

Post a Comment