I am currently working on a CMS based Android Application to allow school students to study and prepare for their exams using their android devices. However prolonged exposure to blue light can have an adverse effect on their eyes. Is there a way to programatically enable night light while the application is being used in the android device and return to normal settings when the application is closed?
Asked
Active
Viewed 542 times
2
-
Your question is not well organized, Like why you want to turn it off while the application is not in use? – Shoaib Kakal Jul 16 '20 at 16:26
-
3Does this answer your question? [How to enable night mode programmatically?](https://stackoverflow.com/questions/47495534/how-to-enable-night-mode-programmatically) – Shoaib Kakal Jul 16 '20 at 16:28
-
I need the night light feature, not the night mode. My app is using android webview at certain places that makes night mode rendered useless for me .. – Paras Rawat Jul 16 '20 at 16:31
1 Answers
1
1st Option You can use material theme to do this.
implementation 'com.google.android.material:material:1.1.0'
if you want your app to follow system settings to can go to styles.xml and change theme to
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
2nd Option if you want to toggle night mode and day mode then you can do this before setContentView(your_layout) for turning on...
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
and for turning off..
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

Rajnish Sharma
- 390
- 2
- 14
-
I am using webview in my android application. AppCompatDelegate.MODE_NIGHT_YES does not work with webview. I need the night light feature to be activated in the device during the time when application is being used.. – Paras Rawat Jul 16 '20 at 16:35
-
Can you give any app name which uses night light so that I can understand the concept of night light better. – Rajnish Sharma Jul 16 '20 at 16:38
-
1Try this one if it answers your question https://source.android.com/devices/tech/display/night-light – Rajnish Sharma Jul 16 '20 at 16:40
-
-
-
No I am not familiar with night night but I found this doc. But anyways people on stack overflow will only guide you with concepts, you should implement it by your own! – Rajnish Sharma Jul 16 '20 at 16:49