• 9

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

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 problem though because my app requires certain things to be colored programmatically, and I can't figure out how to check if the app considers itself in night or day mode. Without that, I can't set a flag to choose the right colors.

Calling AppCompatDelegate.getDefaultNightMode() just returns AppCompatDelegate.MODE_NIGHT_AUTO which is useless.

I don't see anything else that would tell me, but there must be something?

int nightModeFlags =
    getContext().getResources().getConfiguration().uiMode &
switch (nightModeFlags) {
    case Configuration.UI_MODE_NIGHT_YES:

    case Configuration.UI_MODE_NIGHT_NO:

    case Configuration.UI_MODE_NIGHT_UNDEFINED:
  • 58
Reply Report
      • 2
    • Thanks, that did the trick. I started going down the route of checking a colors string value to see which resource file it was coming from, but this is way better!

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


  • 11
Reply Report

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
  • 3
Reply Report

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


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


<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.

  • 2
Reply Report