Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

java - How to detect phone system is dark theme or light theme?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I just found it

Maybe this can help you

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

case Configuration.UI_MODE_NIGHT_NO:
     doStuff();
     break;

case Configuration.UI_MODE_NIGHT_UNDEFINED:
     doStuff();
     break;
}

OR you can go in this link

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...