2

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?

  • 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
  • 3
    Does 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 Answers1

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