Dark theme

Benefits of dark theme:

  1. Can reduce power usage by a significant amount depending on the device’s screen technology.
  2. Improves visibility for users with low vision and those who are sensitive to bright light.
  3. Makes it easier for anyone to use a device in a low light environment.

How to enable dark theme :

  1. Use the system settings to enable dark theme ( settings -> display -> theme )
  2. Another way is providing a switch inside the application to switch between dark and light themes.

    To enable dark mode :
                      AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)

    To disable dark mode :

    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)

To support dark themes in your app first set your app parent theme to

<style name="AppTheme" parent="Theme.AppCompat.DayNight">

You can also use

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">

Next create themes.xml ( night ) file in the values resource folder.

Right click on values folder -> new values resource file -> create file with name themes and add night mode qualifier.

Dark theme is available in android 10 and higher.

Inside the themes.xml ( night ) file you can set configure the themes for the night mode.

Points to remember to support dark theme :

  1. Use system provided styles which automatically supports both dark and light themes.
  2. If you are going to use a custom theme for a view, make sure to define the theme for dark mode as well.
  3. Avoid hardcoding text and background colors and use appropriate theme attributes.

Hope this blog helps you in adding support for dark theme in your application. Check out the other blogs to know more about best practices in android.

Happy learning
Team Appmetry

Leave a Reply