1# Night Mode (aka Dark Theme) on Chrome Android 2 3Night mode (aka Dark Theme) enables users to experience UI surfaces, rendered as Android views, in dark colors. All existing or new user-facing features on Chrome Android should implement a night mode variant of the relevant UI surfaces on Milestone M75+. 4 5[TOC] 6 7## Implement night mode UI for new features 8 9### Colors 10 11Colors defined in **color_palette.xml** and **semantic_colors_non_adaptive.xml** are independent of night mode (i.e. will not change in night mode), and are used for color references defined in **values/semantic_colors_adaptive.xml** and **values-night/colors.xml**. 12 13Color references in values/colors.xml will be used for day mode (aka light theme), and also for night mode if the particular color reference is not defined in values-night/colors.xml. Color references in values-night/colors.xml will be used for night mode. 14 15**Example** 16 17In most cases, you should make use of the color references that are already defined in [//src/ui/android/java/res/values/color_palette.xml](https://cs.chromium.org/chromium/src/ui/android/java/res/values/color_palette.xml) and [//src/ui/android/java/res/values/semantic_colors_adaptive.xml](https://cs.chromium.org/chromium/src/ui/android/java/res/values/semantic_colors_adaptive.xml) for your new feature. 18 19However as an example, suppose you got approval from snowflake-team@chromium.org to add a new background color. 20 21In [//src/ui/android/java/res/values/semantic_colors_non_adaptive.xml](https://cs.chromium.org/chromium/src/ui/android/java/res/values/semantic_colors_non_adaptive.xml), add non-adaptive colors: 22```xml 23<color name="new_bg_color_light">some light background color</color> 24<color name="new_bg_color_dark">some dark background color</color> 25``` 26 27In [//src/ui/android/java/res/values/semantic_colors_adaptive.xml](https://cs.chromium.org/chromium/src/ui/android/java/res/values/semantic_colors_adaptive.xml), add adaptive colors in light mode: 28```xml 29<color name="new_bg_color">@color/new_bg_color_light</color> 30``` 31 32In [//src/ui/android/java/res/values-night/colors.xml](https://cs.chromium.org/chromium/src/ui/android/java/res/values-night/colors.xml), add adaptive colors in night mode: 33```xml 34<color name="new_bg_color">@color/new_bg_color_dark</color> 35``` 36 37An example to use this color in XML: 38```xml 39<View 40 ... 41 android:background="@color/new_bg_color" /> 42``` 43 44An example to use this color in Java: 45```java 46mView.setBackgroundResource(R.color.new_bg_color); 47mView.setBackgroundColor( 48 ApiCompatibilityUtils.getColor(mView.getResources(), R.color.new_bg_color)); 49``` 50 51Optionally, if the color is used exclusively for your feature, or if you want to easily update this color for your feature in the future, in the values/colors.xml that contains colors specifically for your feature, add 52```xml 53<color name="my_shiny_new_feature_bg_color">@color/new_bg_color</color> 54``` 55 56If your feature needs colors that don't change based on day/night mode (e.g incognito mode UI), in the values/colors.xml that contains colors specifically for your feature, reference the colors defined in semantic_colors_non_adaptive.xml if possible. If it is not defined in semantic_colors_non_adaptive.xml, ask snowflake-team@chromium.org for approval of adding a new color in semantic_colors_non_adaptive.xml. There is no need to define the color reference in values-night/colors.xml. 57 58```xml 59<!-- Dark background color for my shiny new feature regardless of day/night mode. --> 60<color name="my_shiny_new_feature_bg_color_dark">@color/new_bg_color_dark</color> 61``` 62 63### Styles 64 65Colors used in styles can be either adaptive or independent of night mode. When using existing or adding new styles, make sure the colors used in the styles fit your need. 66 67**Best practice of naming styles** 68 69* If the color adapts for night mode, avoid mentioning a specific color in the style name since it may not be accurate in night mode. 70```xml 71<!-- OK --> 72<style name="TextAppearance.Headline.Primary"> 73 <!-- default_text_color is dark grey in day mode, and white in night mode. --> 74 <item name="android:textColor">@color/default_text_color_list</item> 75</style> 76 77<!-- NOT OK --> 78<style name="TextAppearance.DarkGreyHeadline"> 79 <!-- default_text_color is dark grey in day mode, and white in night mode. --> 80 <item name="android:textColor">@color/default_text_color</item> 81 ... 82</style> 83 84<!-- OK --> 85<style name="TextAppearance.ButtonText.Blue"> 86 <!-- some_blue_color is dark blue in day mode, and light blue in night mode. --> 87 <item name="android:textColor">@color/some_blue_color</item> 88</style> 89``` 90* If independent of night mode, mention a specific color or where it is generally used. Suffixed by "Light" or "Dark" if the style is inherited from an adaptive style. 91 92```xml 93<!-- OK --> 94<style name="TextAppearance.Body.Incognito"> 95 <item name="android:textColor">@colors/default_text_color_light</item> 96 ... 97</style> 98 99<!-- OK --> 100<style name="TextAppearance.Headline.Primary.Light"> 101 <item name="android:textColor">@color/default_text_color_light_list</item> 102</style> 103<style name="TextAppearance.Headline.Primary.Dark"> 104 <item name="android:textColor">@color/default_text_color_dark_list</item> 105</style> 106``` 107 108### Themes 109 110If adding a new theme, make sure the parent (or any indirect ancestor) theme of the new theme is one of the AppCompat DayNight themes (prefixed with `Theme.AppCompat.DayNight`), or alternatively, define the same theme in values-night/ with the desired parent theme for night mode. See [dark theme](https://developer.android.com/preview/features/darktheme) in Android developer guide for more details. 111 112### Troubleshooting 113 114* Make sure `View` is inflated from `Activity` context instead of `Application` context 115 * `RemoteView` is an exception. See [RemoteViewsWithNightModeInflater.java](https://cs.chromium.org/chromium/src/chrome/android/java/src/org/chromium/chrome/browser/night_mode/RemoteViewsWithNightModeInflater.java?) for details. 116* Make sure color resources are accessed from `Activity` or `View` context instead of `Application` context 117* Check whether `Configuration.uiMode & UI_MODE_NIGHT_MASK` gives the correct UI night mode 118 * If uiMode is not correct, it could be a support library issue or an Android framework issue. You can contact chrome-android-app@chromium.org for help. 119 120## Test new features in night mode 121### Automatic Testing 122 123Render tests are the recommended way to verify the appearance of night mode UI. If you are not familiar with render tests, please take a look at [render test instructions](/ui/android/javatests/src/org/chromium/ui/test/util/RENDER_TESTS.md) to learn about how to write a new render test and upload golden images. 124 125**For tests using DummyUiActivity:** 126 127* Put all the render tests into a separate test suite 128* Use class parameter [`NightModeTestUtils.NightModeParams.class`](https://cs.chromium.org/chromium/src/chrome/android/javatests/src/org/chromium/chrome/browser/night_mode/NightModeTestUtils.java?type=cs&q=NightModeTestUtils.NightModeParams) 129* Pass in a boolean parameter that indicates night mode state in constructor 130* Set up night mode in constructor by calling [`NightModeTestUtils#setUpNightModeForDummyUiActivity()`](https://cs.chromium.org/chromium/src/chrome/android/javatests/src/org/chromium/chrome/browser/night_mode/NightModeTestUtils.java?type=cs&q=setUpNightModeForDummyUiActivity&sq=package:chromium) and [`RenderTestRule#setNightModeEnabled()`](https://cs.chromium.org/chromium/src/chrome/test/android/javatests/src/org/chromium/chrome/test/util/RenderTestRule.java?type=cs&q=setNightModeEnabled) 131* During [`tearDownTest()`](https://cs.chromium.org/chromium/src/ui/android/javatests/src/org/chromium/ui/test/util/DummyUiActivityTestCase.java?type=cs&q=tearDownTest), reset night mode state by calling [`NightModeTestUtils#tearDownNightModeForDummyUiActivity()`](https://cs.chromium.org/chromium/src/chrome/android/javatests/src/org/chromium/chrome/browser/night_mode/NightModeTestUtils.java?type=cs&q=tearDownNightModeForDummyUiActivity) 132 133See [this CL](https://chromium-review.googlesource.com/c/chromium/src/+/1613883) as an example 134 135**For tests using ChromeActivityTestRule:** 136 137* In the method annotated with `@BeforeClass`, initialize states by calling [`NightModeTestUtils.setUpNightModeBeforeChromeActivityLaunched()`](https://cs.chromium.org/chromium/src/chrome/android/javatests/src/org/chromium/chrome/browser/night_mode/NightModeTestUtils.java?type=cs&q=setUpNightModeBeforeChromeActivityLaunched) 138* Add method `setupNightMode()` with annotation `@ParameterAnnotations.UseMethodParameterBefore(NightModeTestUtils.NightModeParams.class)` 139* In method `setupNightMode()`, set up night mode state by calling [`NightModeTestUtils#setUpNightModeForChromeActivity()`](https://cs.chromium.org/chromium/src/chrome/android/javatests/src/org/chromium/chrome/browser/night_mode/NightModeTestUtils.java?type=cs&q=setUpNightModeForChromeActivity) and [`RenderTestRule#setNightModeEnabled()`](https://cs.chromium.org/chromium/src/chrome/test/android/javatests/src/org/chromium/chrome/test/util/RenderTestRule.java?type=cs&q=setNightModeEnabled) 140* In the method annotated with `@AfterClass`, reset night mode state by calling [`tearDownNightModeAfterChromeActivityDestroyed`](https://cs.chromium.org/chromium/src/chrome/android/javatests/src/org/chromium/chrome/browser/night_mode/NightModeTestUtils.java?type=cs&q=tearDownNightModeAfterChromeActivityDestroyed) 141 142See [this CL](https://chromium-review.googlesource.com/c/chromium/src/+/1656668) as an example 143 144### Manual Testing 145 146Different ways to turn on night mode: 147 148* Go to Chrome **Settings -> Themes** on Android L+ 149* Turn on power save mode (aka **battery saver**) on Android L+ 150* Go to **Android Settings -> Developer options -> Night mode** on Android P 151* Go to **Android Settings -> Display -> Theme** on Android Q 152 153Ways to turn on night mode on **custom tab**: 154 155* Turn on power save mode (aka **battery saver**) on Android P+ 156* Go to **Android Settings -> Developer options -> Night mode** on Android P 157* Go to **Android Settings -> Display -> Theme** on Android Q 158* [Set color scheme](https://cs.chromium.org/chromium/src/third_party/android_sdk/androidx_browser/browser/src/main/java/androidx/browser/customtabs/CustomTabsIntent.java?) to `COLOR_SCHEME_DARK` on creating a `CustomTabsIntent.Builder` 159 160Some tips: 161 162* If building **chrome\_apk**, add `compress_resources = false` in gn args to disable Lemon compression. See [Issue 957286](https://crbug.com/957286) for details. 163* Night mode is only available on L+ 164* Animation is turned off when in power save mode on Andoird L-O 165 166## Optional: Add independent night mode control to an Activity 167Most of the features will follow the app-wise night mode control, but some features might require introduction of an independent night mode control. For example, custom tab will not follow the app-wise night mode control, but instead, will respect the night mode settings from the host app. In such cases, you can 168 1691. Create your own implementation of [`NightModeStateProvider`](https://cs.chromium.org/chromium/src/chrome/android/java/src/org/chromium/chrome/browser/night_mode/NightModeStateProvider.java) 1702. Override [`ChromeBaseAppCompatActivity#createNightModeStateProvier()`](https://cs.chromium.org/chromium/src/chrome/android/java/src/org/chromium/chrome/browser/ChromeBaseAppCompatActivity.java?type=cs&q=createNightModeStateProvider) 1713. Use [`AppCompatDelegate#setLocalNightMode()`](https://developer.android.com/reference/android/support/v7/app/AppCompatDelegate.html#setLocalNightMode(int)) to update night mode in the `Configuration` of the `Activity` 172