Marat
Marat

Reputation: 6703

How to get colors from color.xml in values-night folder even in light theme?

I have one page where I need to use colors from night mode regardless of current app/phone theme.

Let's say I have colorAppBg declared in both values/colors.xml and values-night/colors.xml with different hex.

How can I get colorAppBg from values-night folder for 1 specific fragment?

Upvotes: 1

Views: 258

Answers (1)

Hossam Waziry
Hossam Waziry

Reputation: 61

That's the solution:

fun getNightModeColor(@ColorRes colorResId: Int, context: Context): Int {
val nightModeContext = context.createConfigurationContext(
    Configuration(context.resources.configuration).apply {
        uiMode = Configuration.UI_MODE_NIGHT_YES
    }
)
return ContextCompat.getColor(nightModeContext, colorResId)}

Upvotes: 0

Related Questions