Is there a way to use -night resources in old android?

1.1k views Asked by At

I want to implement night mode for my application, but don't want to use any compat-libraries. I place resources in values-night folder and use next code:

UiModeManager systemService = (UiModeManager) this.getSystemService(UI_MODE_SERVICE);
systemService.setNightMode(UiModeManager.MODE_NIGHT_YES);

According to documentation it works on API >22 and it does. Is there a workaround to force using resources from -night folder on APIs 15-22?

2

There are 2 answers

0
lionscribe On BEST ANSWER

It is supported by the AppCompat library. I have no idea why you don't want to use it. I am quoting from Chris Bane's Blog

As you may have seen in the Support Lib 23.2.0 blog post, AppCompat now has a new theme family: Theme.AppCompat.DayNight.

What these themes do is switch between Theme.AppCompat (dark) and Theme.AppCompat.Light (light) based on the time of day. This has many benefits for your users, especially if you’re a content app (it seems to have become a standard feature in Reddit clients). One thing to note is that this feature only actually has an effect when running on an API v14 and later device, on devices before that it will default to the light theme.

0
Ben P. On

From the documentation for UiModeManager.setNightMode():

Note: On API 22 and below, changes to the night mode are only effective when the car or desk mode is enabled on a device. Starting in API 23, changes to night mode are always effective.

Perhaps you can use one of these UI modes?