Android - How to detect if night mode is on when using AppCompatDelegate.MODE_NIGHT_AUTO

前端 未结 5 665
天涯浪人
天涯浪人 2020-12-01 02:53

I\'m using Androids built in day/night mode functionality and I\'d like to add an option to my app for AppCompatDelegate.MODE_NIGHT_AUTO

I\'m having a p

相关标签:
5条回答
  • 2020-12-01 03:15

    If you are kotlin developer then you can use below code to judge dark mode.

     val mode = context?.resources?.configuration?.uiMode?.and(Configuration.UI_MODE_NIGHT_MASK)
        when (mode) {
            Configuration.UI_MODE_NIGHT_YES -> {}
            Configuration.UI_MODE_NIGHT_NO -> {}
            Configuration.UI_MODE_NIGHT_UNDEFINED -> {}
        }
    

    For more about dark theme mode

    https://github.com/googlesamples/android-DarkTheme/

    0 讨论(0)
  • 2020-12-01 03:16

    The bitwise operator in Java (which is used in @ephemient 's answer) is different in kotlin so the code might not be easily convertible for beginners. Here is the kotlin version just in case someone needs it:

        private fun isUsingNightModeResources(): Boolean {
            return when (resources.configuration.uiMode and 
    Configuration.UI_MODE_NIGHT_MASK) {
                Configuration.UI_MODE_NIGHT_YES -> true
                Configuration.UI_MODE_NIGHT_NO -> false
                Configuration.UI_MODE_NIGHT_UNDEFINED -> false
                else -> false
        }
    }
    
    0 讨论(0)
  • 2020-12-01 03:18
    int nightModeFlags =
        getContext().getResources().getConfiguration().uiMode &
        Configuration.UI_MODE_NIGHT_MASK;
    switch (nightModeFlags) {
        case Configuration.UI_MODE_NIGHT_YES:
             doStuff();
             break;
    
        case Configuration.UI_MODE_NIGHT_NO:
             doStuff();
             break;
    
        case Configuration.UI_MODE_NIGHT_UNDEFINED:
             doStuff();
             break;
    }
    
    0 讨论(0)
  • 2020-12-01 03:19

    A very simple solution is to add a string resource like this.

    res/values/strings.xml

    <string name="mode">Day</string>
    

    res/values-night/strings.xml

    <string name="mode">Night</string>
    

    And then wherever you need to check it:

    if (resources.getString(R.string.mode) == "Day") {
        // Do Day stuff here
    } else {
        // Do night stuff here
    }
    

    But you CAN NOT call this before super.onCreate() in an activity's life-cycle. It will always return "Day" before onCreate.

    0 讨论(0)
  • 2020-12-01 03:27

    For Xamarin.Android

    if (Build.VERSION.SdkInt >= BuildVersionCodes.Froyo)
    {
        var uiModeFlags = Application.Context.Resources.Configuration.UiMode & UiMode.NightMask;
        switch (uiModeFlags)
        {
            case UiMode.NightYes:
                // dark mode
                break;
            case UiMode.NightNo:
                // default mode
                break;
            default:
                // default mode
                break;
        }
    }
    else
    {
    // default mode
    }
    
    0 讨论(0)
提交回复
热议问题