How to enable night mode programmatically?

后端 未结 3 440
别跟我提以往
别跟我提以往 2020-12-15 06:44

I am looking for a way to enable night mode programmatically with an Android code:

public static void setNightMode(Context target , boolean state){

    UiM         


        
相关标签:
3条回答
  • 2020-12-15 07:05

    SIMPLEST SOLUTION

    You can enable/disable dark theme just by:

    1. enable dark theme:

      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
      
    2. forcefully disable dark theme:

      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
      
    3. set app theme based on mobile settings of dark mode, i.e. if dark mode is enabled then the theme will be set to a dark theme, if not then the default theme, but this will only work in version >= Android version Q

      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
      

    Notes:

    1. Your base theme for app/activity should be

    "Theme.AppCompat.DayNight"

    like

    <style name="DarkTheme" parent="Theme.AppCompat.DayNight">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    
    1. Your res folder's names would end with -night so that different colors and images you can set for day and night themes like

    drawable & drawable-night,
    values & values-night

    0 讨论(0)
  • 2020-12-15 07:22

    Make sure to change the default theme from Theme.AppCompat.Light.DarkActionBar to Theme.AppCompat.DayNight.DarkActionBar in the styles.xml file and then do AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) to switch to the night mode. I have tested it in APIv23(Android 6.0) and above and it is working fine. For a better explanation see this codelab by Android

    0 讨论(0)
  • 2020-12-15 07:30

    NightOwl has its own implementation for switching day/night mode on Android. Getting started with NightOwl is super easy. Here's a code snippet:

    Init the NightOwl in Application class,

    NightOwl.builder().defaultMode(0).create();
    

    Call three method in your Activity class,

    public class DemoActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // step1 before super.onCreate
            NightOwl.owlBeforeCreate(this);
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_demo);
    
            // step2 after setContentView
            NightOwl.owlAfterCreate(this);
    
            // write your code
        }
    
        @Override
        protected void onResume() {
            super.onResume();
    
            // step3 onResume
            NightOwl.owlResume(this);
        }
    
    }
    

    Switch skin everywhere as you like,

    View v = findViewById(R.id.button);
    v.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            NightOwl.owlNewDress(SettingActivity.this);
        }
    });
    
    0 讨论(0)
提交回复
热议问题