Last active
May 9, 2018 19:36
-
-
Save dellisd/e7064525bf1205f21a2e0aebbe792779 to your computer and use it in GitHub Desktop.
How to check for night mode without using AppCompat.DayNight theme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ca.llamabagel.daynight; | |
import android.content.res.Configuration; | |
import android.os.Bundle; | |
import android.os.PersistableBundle; | |
import android.support.annotation.Nullable; | |
import android.support.v7.app.AppCompatActivity; | |
import android.widget.TextView; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) { | |
super.onCreate(savedInstanceState, persistentState); | |
int nightFlag = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; | |
TextView textView = findViewById(R.id.textView); | |
switch (nightFlag) { | |
case Configuration.UI_MODE_NIGHT_NO: | |
textView.setText("Not Night Mode"); | |
break; | |
case Configuration.UI_MODE_NIGHT_YES: | |
textView.setText("Yes Night Mode"); | |
break; | |
default: | |
textView.setText("Probably not Night Mode"); | |
break; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ca.llamabagel.daynight | |
import android.content.res.Configuration | |
import android.support.v7.app.AppCompatActivity | |
import android.os.Bundle | |
import kotlinx.android.synthetic.main.activity_main.* | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
val nightFlag = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK | |
textView.text = when (nightFlag) { | |
Configuration.UI_MODE_NIGHT_NO -> "Not Night Mode" | |
Configuration.UI_MODE_NIGHT_YES -> "Yes Night Mode" | |
else -> "Probably not Night Mode" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment