Android – DayNight View Tutorial

Published by admin on

Android DayNight View Tutorial
What is day night theme and how to use it on android application ?

Support Library 23.2 has introduced a new DayNight theme for Android Apps using which will automatically change the application theme to day or night mode based on the time of day and user’s last known location. Also, this theme can be used back up to Android Ice Cream Sandwich devices. 

android daynightview


Types of Day Night theme :
We can use any one of the following themes on our project :


Separate resources for night mode and day mode :
Sometimes we need to use different resources for different mode . This can be accomplished by using different drawable and different values folders.

a) Folder names for default mode :
b) Folder names for night mode :


In this tutorial, we will use two different drawable folders with two different images having same name. If night mode is enabled in the application, image from drawable-night will be picked up.

Similarly for showing different text colors on different mode , we will use different values folder.
  1. Create one sample project on Android Studio with an Activity as MainActivity
  2. Also, create one more Activity as DayNightActivity . We will add 3 Buttons on this Activity. Clicking on these buttons will Start DayNightActivity with different theme mode. MainActivity and DayNightActivity will look like as below :

    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    public class MainActivity extends AppCompatActivity implements View.OnClickListener{
        private Button dayButton;
        private Button nightButton;
        private Button autoButton;
        protected void onCreate(Bundle savedInstanceState) {
            dayButton = (Button)findViewById(;
            nightButton = (Button)findViewById(;
            autoButton = (Button)findViewById(;
        public void onClick(View view) {
            switch (view.getId()) {
                    //change theme to auto mode
                    //Change theme to day mode
                    //change theme to night mode
            Intent intent = new Intent(MainActivity.this, DayNightActivity.class);


    package com.codevscolor.daynightview;
    import android.os.Bundle;
    public class DayNightActivity extends AppCompatActivity {
        protected void onCreate(Bundle savedInstanceState) {
            TextView title = (TextView) findViewById(;
            int themeMode = AppCompatDelegate.getDefaultNightMode();
            if (themeMode == AppCompatDelegate.MODE_NIGHT_AUTO) {
                title.setText("Current Theme : Auto");
            } else if (themeMode == AppCompatDelegate.MODE_NIGHT_NO) {
                title.setText("Current Theme : Day");
            } else if (themeMode == AppCompatDelegate.MODE_NIGHT_YES) {
                title.setText("Current Theme : Night");

     3.Update your Application’s theme inside style.xml as below :

        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
            <item name="android:textColorPrimary">@color/textColorPrimary</item>

    4. Create two “values” folders : values and values-night . And update colors.xml file on each of these folders:


    <?xml version="1.0" encoding="utf-8"?>
        <color name="colorPrimary">#afb53f</color>
        <color name="colorPrimaryDark">#5b520f</color>
        <color name="colorAccent">#FF4081</color>
        <color name="textColorPrimary">#000000</color>
        <color name="colorBackground">#FFFFFF</color>


    <?xml version="1.0" encoding="utf-8"?>
        <color name="colorPrimary">#3F51B5</color>
        <color name="colorPrimaryDark">#303F9F</color>
        <color name="colorAccent">#FF4081</color>
        <color name="textColorPrimary">#FFFFFF</color>
        <color name="colorBackground">#000000</color>

     If night mode is enabled, the application will use resources from valuesnight folder and if day mode is enabled, it will use from values folder.

          5. Similarly, create two different drawable folders and place two images with same name inside both of these folders.

That’s it. You can see that if Button auto is to enable auto mode, button day is to enable day mode and night is to enable night mode in the application. You can download this application from here , and if you have any doubt, please let me know 🙂

images credit : link1, link2

Related Android posts :


kılıç arslan · July 15, 2018 at 7:24 pm

thx for the great code, but when i implement it on my aplicatioın only the actionbar color changes why do you think it can happen can you help me please?
Thank you very much again for the greaat code:)

admin · July 16, 2018 at 4:46 pm

I have tried it but it is working fine for me. Could you please try again by pulling the code ? I have updated it on Github.

Kılıç Arslan · July 17, 2018 at 10:36 am

it worked this time, my mistakes (not code’s), thank you very much admin:)

    admin · July 17, 2018 at 10:38 am

    you are welcome. keep visiting our blog 🙂

Mbasela Mbasela · November 11, 2019 at 2:09 pm

How do you save the theme so that even if you close the app then open it again the theme remain unchanged-permanent. Until you change it yourself

Leave a Reply

Your email address will not be published. Required fields are marked *