Search code examples
androidandroid-8.0-oreoseekbar-thumb

On Oreo, setColorFilter() on a SeekBar thumb changes all thumbs


I have a class public class LevelSeekBar extends AppCompatSeekBar. Within that class, I have this method:

@Override
public void setEnabled(boolean enabled) {
    super.setEnabled(enabled);
    int color = getResources().getColor(getColorForState(enabled));
    getThumb().setColorFilter(color, PorterDuff.Mode.SRC_IN);
}

This LevelSeekBar is used within a custom list, implemented using a RecyclerView. On a refresh, each LevelSeekBar.setEnabled() is called one after the other.

On devices < Android 8.0 (Oreo), this works completely as expected.

However, on Android 8.0, all of the Thumbs are set based on the last color set in the list. In other words, let's say I have a list of 2 items -- the first one is 'disabled' and the second one is 'enabled' -- both items will show up as enabled.

I have confirmed in the debugger that the 'enabled' variable and the corresponding color returned are correct and as-expected, and I have confirmed that the full recyclerview is being refreshed in this case.

Seems like an Oreo bug. Has anyone else seen this, or have any ideas for how to work-around?


Solution

  • You need to call .mutate() on the Drawable that is returned by getThumb().

    https://developer.android.com/reference/android/graphics/drawable/Drawable.html#mutate()