ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Dark Mode on Android App with Kotlin

--

On the Google I/O 2019, the Google team presented the possibility to developers add a dark theme to the android applications and since then many application has already been updated with this feature like example: Youtube, Instagram, Chrome and so many others.

In this article, I will explain how you can implement dark mode in your application in an easy way.

The benefits 🌚

As you know, the dark mode can bring to people some benefits like:

  • It can reduce power usage by a significant amount.
  • Improves visibility for users with low vision and those who are sensitive to bright light.
  • It makes it easier for everyone to use a device in a low-light environment.

Recommendations 👍🏿

Your themes and styles should avoid hard-coded colors or icons intended for use under a light theme. You should use theme attributes (preferred) or night-qualified resources instead.

You might want to allow users to change the app’s theme while the app is running. Your app can let the user choose between themes considering the following recommendations:

When running on devices running Android 9 or earlier, the recommended theme options are:

  • Light
  • Dark
  • Set by Battery Saver (the recommended default option)

When running on Android 10 (API level 29) and higher, the recommended options are different, to allow the user to override the system default:

  • Light
  • Dark
  • System default (the recommended default option)

Let’s code 👨🏿‍💻

First, go ahead, and create a new Android project.

In the activity_main.xml file, we will have just one Textview to show some text and one Button to allow us to change the theme.

As you see when running the app

light theme

After that, we need to change the android style to (in my case)

Or to the material component:

Now let’s go to the MainActivity Class and create the method chooseThemeDialog and inside add the code to create an AlertDialog, to allow the user to choose the theme.

In the onCreate method let’s call the chooseThemeDialog method when the user clicks on the button

Run the app again and let’s see the result🧐 when we click on the button

light theme

So, let’s go inside to chooseThemeDialog method and add the code to change the theme and apply to our App using the when condition and AppCompatDelegate.

AppCompatDelegate is a class in support library of android that allows us to work with Dark Theme implementation. for that's work we will com a static method call setDefaultNightMode and pass the mode as a parameter, like:

  • AppCompatDelegate.MODE_NIGHT_NO: means night mode is not active, we’re using the light theme
  • AppCompatDelegate.MODE_NIGHT_YES: means night mode is active and we’re using dark theme
  • AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM: means we will follow the system definition

Run the app and check the result when we click on dark on the AlertDialog

dark theme

But we can improve the code, by saving the choice of the user, in this case, we can use SharePreferences for this.

For it works, we need do added the new SharedPreferences dependency to the build.gradle file.

Let’s create the class MyPreferences to help us manipulate and store the value on the SharedPreferences.

Now we can update our chooseThemeDialog method to save the choice of the user and keep the theme even when we close the app

Create also the checkTheme method to check on the onCreate method what theme status we saved on SharedPreferences

Now as we already said, we need to call the checkTheme method inside the onCreate.

Now the final result🎉

app demo
App Demo

So, that is it for now, and don’t forget to see the full code on my Github

Thanks to you for reading this post! Please do 👏🏿 if you liked it and want more posts about android development.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Written by Manuel Ernesto

👨🏿‍💻 Kotlin 🟣 Android 🟢

Responses (3)

Write a response