Here is the code for the detect dark/light theme when we using the application.
public void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int mSysThemeConfig = newConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK;
layout = findViewById(R.id.mainLayout);
switch (mSysThemeConfig) {
//Light theme
case Configuration.UI_MODE_NIGHT_NO:
layout.setBackgroundResource(R.drawable.lightTheme);
layout.setLayoutParams(new RelativeLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
break;
//Dark theme
case Configuration.UI_MODE_NIGHT_YES:
layout.setBackgroundResource(R.drawable.darkMode);
layout.setLayoutParams(new RelativeLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
break;
}
}
How do I detect the dark/light theme when we start an application?
question from:
https://stackoverflow.com/questions/65912537/how-to-detect-phone-system-is-dark-theme-or-light-theme 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…