-1

I need to know if night mode is activated, with a boolean or int, I found a boolean that detects but it only works from andorid R isnightmodeactive () and I can't find another way to know if night mode is activated or not, any idea

I did this test but I continue as before it does not throw anything, I do not know how to know what mode my device is in, night or day

    public double isDark(int a) {
    return a;
     }  

 public void nightmode(){

            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
            if(AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES){
                isDark(1);
            }else if(AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_NO){
                isDark(2);
            }

}

jose moya
  • 35
  • 5

1 Answers1

1

Google it or search on developer.android.com. I found a similar question on StackOverflow. Here is the kotlin code from that question

fun Context.isDarkThemeOn(): Boolean{
    return resources.configuration.uiMode and 
            Configuration.UI_MODE_NIGHT_MASK == UI_MODE_NIGHT_YES
}
  • 1
    How would that be in java – jose moya Jun 15 '20 at 06:20
  • @jose moya, see this article -> https://blog.mindorks.com/how-to-convert-a-kotlin-source-file-to-a-java-source-file –  Jun 15 '20 at 12:55
  • 1
    I already did it but nothing happens, it does not give me an int or boolean that tells me if the phone is activated or in night mode – jose moya Jun 15 '20 at 14:34
  • It should return true or false or UI mode as a string or any other type. It **should** return something. –  Jun 15 '20 at 14:36
  • Download fernflower.jar from https://jar-download.com/artifacts/org.jboss.windup.decompiler/decompiler-fernflower/3.0.0.Final/source-code –  Jun 15 '20 at 14:43
  • 1
    You can see what I tried, I just edited my original post I still don't get any data – jose moya Jun 15 '20 at 17:11
  • so, try this code: `public boolean Context.isDarkThemeOn(){ return Configuration. Configuration.UI_MODE_NIGHT_MASK == UI_MODE_NIGHT_YES; }` –  Jun 16 '20 at 06:26
  • Sorry, I am not an expert at android but an expert @ java. I have some programming experience. So @josemoya, see these StackOverflow questions. https://stackoverflow.com/questions/44170028/android-how-to-detect-if-night-mode-is-on-when-using-appcompatdelegate-mode-ni || https://www.reddit.com/r/androiddev/comments/d1rgwl/detecting_android_q_dark_mode/ –  Jun 16 '20 at 06:32