Search code examples
androidkotlinandroid-drawable

How can i change icon.png depending on app theme (day/night) android kotlin


I have a logo on my splash screen and two files: logo_night.png and logo_day.png

How can i change this logo that will change depending on the theme of the app using these two files?


Solution

  • The usual way is to use a night resource qualifier, so if you're using a dark theme it pulls the named resource from the equivalent night folder if it exists there.

    You have three basic options with this I think:

    • just call them both logo.png, but put one version in drawable and the other in drawable-night. That way, whenever you refer to @drawable/logo it'll pull the correct version depending on the theme. If you have multiple density versions, in drawable-hdpi etc, you'll need them in drawable-night-hdpi etc. (the qualifier order matters)

    • if you're making your splash in the usual way, where you create a splash drawable with a background layer and your logo drawable on another layer, you could just make a night variant of this instead (in drawable-night). Each version can explicitly refer to logo_day or logo_night and you can just keep those PNGs in the normal drawable folder

    • or you can make a night version of your splash theme, and explicitly set the background to the dark version of your splash drawable instead. Both your splash drawables can stay in the normal drawable folder

    So basically, something's gotta get a version in a night-qualified resource folder, so it can provide an alternative resource for dark mode. It's up to you which resource in that hierarchy you want to make the night version of (or you can do it for all of them if you want).

    Also bear in mind that your splash theme (if you're doing it that way) follows the system's dark mode setting. If you're implementing a toggle in your app, and you set it to dark mode, the splash will still show up as the light variant if that's what the device is set to. That theme is set before your app starts and gets to work out if it should be in dark or light mode