Skip to content

Coil rememberImagePainter incorrect dark theme drawable resource resolution #954

Description

@kenneth-leong-gt

Describe the bug
There seems to be a bug with the drawable resource resolution when rememberImagePainter is used with a Drawable resource Id. When i set my device to dark theme via the android system setting, everything works as per normal. The problem comes when i am setting dark theme for my app is distinct from what the system setting is, the issues will come.

All my other UI element's color are changing properly when switching from light to dark mode no matter the combination of system and app setting. It seems that its only the drawable id resolution.

I am not sure if this is an issue with accompanist drawable painter or coil, most probably its with accompanist drawable painter since coil 1.4.0 s relying on accompanist drawable painter now.

The below table should illustrate the issue more clearly.

System →
App ↓
Light Theme Dark Theme
Light Theme light drawable ✅ dark drawable ❌
Dark Theme light drawable ❌ dark drawable ✅

To Reproduce
1 - Set system to light theme.
2 - Set app to dark theme via AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
3 - Go to a screen that display a drawable with resources in drawable and drawable-night folders, the drawable will be resolved to the one in drawable and displayed.

the reverse case can also be observed.
Bottom line is it seem the drawable resolution seems to be resolving to the System theme setting and not the application context theme.

My current work around would be to do something like

val drawable = ContextCompat.getDrawable(LocalContext.current, imageDrawableResId)
val painter = rememberImagePainter(data = drawable)  // ✅ this will work well
// val painter = rememberImagePainter(data = imageDrawableResId)  // ❌ this will be the problem           

This painter will get the correct drawable.

Version
Coil-compose: 1.4.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions