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

前端 未结 5 683
天涯浪人
天涯浪人 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:19

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

    res/values/strings.xml

    Day
    

    res/values-night/strings.xml

    Night
    

    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.

提交回复
热议问题