Changing Android icon color with setColorFilter() usually works, but not always

563 Views Asked by At

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?

1

There are 1 best solutions below

0
On

This post mentioning issues with view.invalidate (slowness, not lack of response) mentions that you could use a TextureView.

This post notes you can change the color by placing the TextureView in its own layout and then change the background color.

This answer to a similar question notes that you may need to post the change to the views message queue on later APIs (21+). Notably, the general approach of setColorFilter(Color, PorterDuff.Mode.SRC_ATOP) seems to be preferred by a lot of app makers. Answers related to older Samsung hardware in the same thread also note "applying it in the draw(Canvas canvas) method" or using DrawCompat with setTint and setTintMode.

Finally, because its only the S22+, it may also be a specific issue with the phone. There have been a lot of complaints about video and image rendering on the S22+ and a hidden game optimizing service that cause stutter, lag, and graphical draw bugs. Samsung is apparently also having issues pushing updates correctly.