Skip to content

How to use day Night Theme ?

Devrath edited this page May 28, 2021 · 1 revision

Steps in setting up the day-night theme

If we want to support dark theme on android-10 & above, pre android devices we need to use via day-night theme

We have two options

DayNight Theme

  • The androidX AppCompact library has a DayNight theme called Theme.AppCompat.DayNight
  • In the res/values/styles.xml
<style name="Theme.AppCompat.DayNight" parent="Theme.AppCompat.Light">
  • In the res/values-night/styles.xml
<style name="Theme.AppCompat.DayNight" parent="Theme.AppCompat">
  • Styles implemented as
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompact.DayNight">
  <!-- 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:forceDarkAllowed">true</item>
</style>

MaterialComponents DayNight Theme

  • The Material component library also has theme support supporting both day and night types.
  • Advantage of using the MaterialComponents is the appearance that looks more stylish and modern which users might expect from a modern android application.
  • For this we need to use
implementation 'com.google.android.material:material:1.4.0-beta01'
  • Styles added as
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
  <!-- 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:forceDarkAllowed">true</item>
</style>