Android DayNight Theme for Night Mode in App With Examples

In this tutorial, we’ll be discussing and using the Android DayNight theme in our application. If you have an app with reading materials then having night mode is helpful for ease of eyes.

Android DayNight Theme

Android released a new theme: Theme.AppCompat.DayNight with the support library 23.2.0.

Thanks to this theme, we can now toggle between the light and dark modes of our application. We can do so manually or let Android detect the time of the day implicitly from your phone.

This theme enhances the readability and usability of your application during the night by replacing the white flashy background with a darker one. Many reader applications have already deployed this theme in their apps.

Let’s get started with our implementation by creating a new Android Studio project with empty activity.

Adding the theme to our styles.xml

Let’s replace the current theme in our application with the DayNight one.

To set the DayNight theme in our application we use the method: AppCompatDelegate.setDefaultNightMode()

Following are the arguments allowed in the above method.

  • MODE_NIGHT_YES – Enables night mode manually.
  • MODE_NIGHT_NO – Disables night mode manually.
  • MODE_NIGHT_FOLLOW_SYSTEM – Uses the system settings to determine the time of day and toggles NightMode accordingly. This is the default argument.
  • MODE_NIGHT_AUTO – This tries to auto-detect the time from the device location APIs. If the runtime permission for location services isn’t granted, then it uses the system time.

Add the following code in the onCreate() method.

The theme should always be set before the setContentView method is invoked.

What is AppCompatDelegate?

An AppCompatDelegate is a class represents a delegate which you can use to extend AppCompat’s support to any Activity.
Let’s see how our activity screen looks like with the day mode and night mode enabled one by one.

android-progress-bar-demo

android-nightmode-hello-world-1

The TextView changes its color to white in the night mode. This is since the TextView implicitly contains the default style named : ?attr/colorPrimary which toggles the color based on the light/dark app theme. If you set a custom color @color/red on the TextView, it won’t change between day/night modes.

The Toolbar text color in the day mode is black. How to set it to white in the styles.xml itself?

To retrieve the current night mode type we use the method AppCompatDelegate.getDefaultNightMode() which returns an integer for each of the types discussed earlier, respectively.

Having got a basic idea let’s make an application which will:

  • Customise resources, styles in day/night modes.
  • Toggle DayNight theme from the UI
  • See how various UI widgets look in Night Mode.

Android Night Mode Project Structure

android-staticmaps-project

Android DayNight Theme Example Code

The code for the activity_main.xml class file is given below.

  • We’ve set a custom text color and drawable on the ImageView.
  • To set different colors and drawables for day and night themes, we need to create separate folders for the resources.
  • The day theme resources reside in the default directory.
  • The night theme resources reside in folders with names appended with -night.
  • Hence we’ve created values-night and drawable-night folders in our project.
  • The drawable filename, colors, style names must be the same in both the directories for the resources you wish to toggle in DayNight theme.
  • If the above things are defined in only one of the directories, the same would be used in day and night themes.

The code the styles.xml in values and values-night folders are given below.

The styles defined above are used to set customise the standard DayNight theme.

The respective things for the colors.xml are defined as shown below.

android-daynightmode-colors-nightmode

 

colors.xml from values-night folder

android-daynightmode-colors-nightmode

 

colors.xml from values folder

The code for the MainActivity.java class is given below.

In the above code we use the Switch to toggle between the day and night mode themes in our application.
We save the current mode in a SharedPreferences object.

WHY?

The theme of an activity can be set only once. Hence when the switch is toggled, we need to save the new mode in a SharedPreference object. We use the Singleton Pattern for Application class. This way the same instance of Application class can be used throughout the application.

The code for the InitApplication.java class is given below.

It is here where we update and retrieve the night mode type from the Shared Preferences.

Android Night Mode Support App Output

android daynight theme output

You can download the final Android DayNight Mode Project from the link below.

By admin

Leave a Reply

%d bloggers like this: