How to make Dark (Night) Mode Theme in Android

Hello World, today we are going to see how we can implement a dark theme or night mode in our android application. This tutorial is going to be very simple and easy to understand. The dark theme is attractive to users and it is comfortable for low light conditions. Recently many apps adapt dark mode in their app and the output of the night mode is amazing as many users love dark mode for their app. An example of a dark theme is Whatsapp dark mode in android see the below image.

Whatsapp dark mode

Let’s look at how our app will look like, see the below gif for our end result

Let’s see how we can implement dark theme in our app.

Make layout for dark theme

First, we need to make our layout so that we can apply our dark theme to it.

If you see the above gif we used cardview to make our layout.

See the below code for layout.

Now we need to set theme colors in the layout, for example, we need to set the background color of activity and cardview and also we need to set textview color and icon colors. But setting all the colors we need some way to set colors dynamically. 

We want to change the background color of activity to dark black color when we
apply our dark theme in the application.

To make the dynamic values we will need to make attrs.xml file in the values

Make custom attribute values for layout.  

Create a new xml file in the values folder. Right-click on values folder and
click on new then click values resource file and type attrs in the file name

Your values folder should look like below:

In the attrs.xml we will declare our custom attribute values like below.

In the above code, we have defined 7 values and their type which is color. Each
of the values, we will use in our layout file.

But before using these values we need to assign or set these values in our
currently applied theme and also we will make our dark theme and update these
attribute values.

Make Dark theme in styles.xml and set attributes value.

First, we will set attribute values for our default or light theme then we will update those attribute values in the Dark theme. Let’s do this.

In the AppTheme we are setting our attribute value you are free to change as per your need. We are setting default colors. Then we made a new theme called
DarkAppTheme and now this time we are setting all the custom attribute values to the dark theme matching colors.

We have set the app background color to black and other background colors to
dark colors and set text and icon colors according to the dark theme.

Now the time to use these values in our layout. Open your activity XML file
and check the background and textColor values as shown below.

If you noticed background and text color values, see how we use the attribute
values in the activity layout so that these take values from the currently
applied theme and set we set DarkAppTheme the values of the Dark theme will
update and hence our layout values will also update and our dark theme will be
in use. 

Let’s see our we can switch our theme from the java code.

Set Theme to DarkAppTheme using Java

Now we have completed our XML part now its time to make logic to change the
dark theme in the app. 

To apply the dark theme we need to call the setTheme method before
setContentView function called in the onCreate function of the activity.

If you want to apply a dark theme on click of the button as shown in the above gif. We can use this code.

To toggle the theme we need to check which theme is currently applied in so
that we can change the theme to do that we can use AppCompatDelegate
class which helps us to determine which theme is currently applied by using
its static function setDefaultNightMode.

AppCompatDelegate’s setDefaultNightMode function will save the current theme
configuration and we can check that value and apply our theme according to the
check as we have shown in the above code. 

Now if you launch your app then when you click the button your nice looking
dark theme is ready to rock the world.

If you like this article do share it with your friends and batchmates and
don’t forget to subscribe to our newsletter if you haven’t joined already.

Thanks for reading have a nice day.


Source link