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?