The dark mode is a type of UI theme that contrasts with the light mode commonly used in everyday apps. The dark mode was first developed based on researches and practices in the human-machine interaction field. It does not simply change the page background to black and text content to white, but provides a complete set of app color themes that adapt to the dark mode. The dark mode is generally more soothing than the light mode. It reduces the brightness that can cause eye strain and fatigue for users. In addition, the dark mode reduces the power consumption of apps to some extent and improves the battery life.
Dark mode adaptation to apps must comply with the basic UX design principles to ensure the readability, comfort, and consistency of the app page content. For details, see Design Principles. The app adaptation process includes adaptation of color resources such as font colors and element background colors, adaptation of media resources such as images and icons, and adaptation of the system status bar. In addition, web pages loaded using the Web component need to be processed separately.
This section describes the dark mode adaptation process, and lists common problems and solutions.
After the system is switched to the dark mode, some content in the app may be switched to the dark theme, such as the status bar, background color of the dialog box, and system components. As a result, the display of in-app pages may be in disorder.
To cope with the preceding situation, you need to perform content adaptation in dark mode on your app. Currently, the adaptation mainly depends on the Resource Directories. When the system setting (such as the system language and dark/light mode) changes, the app automatically loads the resource files in the corresponding resource directory.
The system reserves the dark directory for the dark mode. By default, the dark directory does not exist when the app is created. To adapt to the dark mode, you need to manually create the dark directory in src/main/resources and store the resources required by the dark mode in the directory. The resources required by the light mode can be stored in the src/main/resources/base directory by default.
When defining resources, you need to define resources with the same name in the base and dark directories. For example, text_color is set to black in the base/element/color.json file and to white in the dark/element/color.json file. When the dark and light modes are switched, elements that use $('app.color.text_color') as the color value in your app will automatically switch to the corresponding color, without requiring additional logical judgments for control.
Generally, switching between the dark and light modes does not change the structure of the app UI. Instead, the UI structure is the same, but different theme colors and images are used. In this way, the entire app remains aesthetically pleasing after switching to the dark mode. The following figure shows an example of UX adaptation in dark mode.

As shown in Figure 1, the main adaptation items in dark mode adaptation are color resource adaptation, media resource adaptation, and status bar adaptation. If the app uses the Web component to load web pages, web page adaptation to the dark mode is also required.
Currently, there are two methods for switching between dark and light modes in apps.
Use the setColorMode() method to set ColorMode to COLOR_MODE_NOT_SET (no color mode is set). Then, the app can automatically detect the system color mode switching during running. If the app completes the dark and light mode adaptation, it automatically switches to the corresponding color mode.
To switch to the dark mode, call the setColorMode() method to set ColorMode to COLOR_MODE_DARK. To switch to the light mode, set ColorMode to COLOR_MODE_LIGHT.
The following table lists the adaptation items of the dark mode.
Adaptation Item | Adaptation Content | Adaptation Mode |
|---|---|---|
Color resource adaptation | Component background color, font color, and so on |
|
Media resource adaptation | Images and icons used in the app |
|
Status bar adaptation | Different display effects of the status bar in dark and light mode, including the background color and font color of the time in the status bar. | |
Web content adaptation | Web page loaded using the Web component in an app | For details, see Setting Dark Mode. |
Light Mode | Dark Mode |
|---|---|
|
|
In the preceding table, the page effect is normal in light mode, but after the dark mode is used, the contrast between the text in the dialog box and the background color is lower than 5:1. As a result, it is difficult for users to identify the content in the dialog box. The key problem of the preceding effect is that if the background color of a custom dialog box is not manually specified, the system performs dark and light color adaptation on the background color of the dialog box by default. However, the content in the dialog box, especially the custom content, cannot be automatically adapted to the dark mode. Therefore, when the system switches to the dark mode, the background color of the dialog box automatically switches to dark, but the content in the dialog box is in the same color as in light mode. As a result, the content cannot be clearly viewed. There are two methods to solve this problem.
{
"color": [
{
"name": "text_color",
"value": "#000000"
}
]
} {
"color": [
{
"name": "text_color",
"value": "#FFFFFF"
}
]
} @Entry
@Component
struct Index {
private customDialogComponentId: number = 0;
private promptAction = this.getUIContext().getPromptAction();
@Builder
customDialogPositiveExample() {
Column() {
Text($r('app.string.authorization_succeeds'))
.fontColor($r('app.color.text_color'))
// ...
Text($r('app.string.authorization_code'))
.fontColor($r('app.color.text_color'))
Row({ space: 8 }) {
Button($r('app.string.copy'), { buttonStyle: ButtonStyleMode.TEXTUAL })
.fontColor($r('app.color.text_color'))
// ...
Button($r('app.string.confirm'), { buttonStyle: ButtonStyleMode.TEXTUAL })
// ...
}
// ...
}
// ...
}
build() {
// ...
}
} Light Mode | Dark Mode |
|---|---|
|
|
Media resource adaptation is to use media resources such as images or icons in different colors in dark and light modes to achieve better user experience. The following is an example of an effect in which an icon in an app is not adapted to the dark mode. Content that is not adapted is marked by yellow dotted boxes.
Light Mode | Dark Mode |
|---|---|
|
|
The key problem of the preceding incorrect example is that the icons in the app are not adapted to the dark mode. As a result, the icon color remains unchanged in dark and light modes. In dark mode, the contrast between the icon color and the background color is too low, making it difficult for users to see the icons in the app. There are two solutions to this type of problem.
@Component
struct Home {
// ...
build() {
Scroll() {
Column() {
// ...
Stack({ alignContent: Alignment.TopStart }) {
Image($r('app.media.bell'))
.width('100%')
.borderRadius(12)
.objectFit(ImageFit.Cover)
// ...
}
// ...
}
}
// ...
}
} Light Mode | Dark Mode |
|---|---|
|
|
Status bar adaptation means that different background colors and font colors are used for the status bar in dark and light color modes. If the immersive layout is not enabled for the app, the status bar displays black text on a white background in light mode and white text on a black background in dark mode by default. If the immersive layout is enabled, the background color of the status bar is the same as that of the app. By default, the text on the status bar is black in light mode and white in dark mode. In this case, if a dark background is set in light mode or a light background is set in dark mode, the contrast between the background color of the status bar and the font color of the status bar will be too low, resulting in abnormal display. Figure 2 shows the incorrect effect.

The main problem is that the background color of the page is fixed as black. When the system is switched to the light mode, the text in the status bar is switched to black by default. In this case, the background color of the status bar is the same as the text color, and the contrast is too low. As a result, the text in the status bar is invisible. There are two methods to solve this problem.
// src/main/ets/pages/Index.ets
@Entry
@Component
struct Index {
// ...
build() {
Navigation(this.navPathStack) {
// ...
}
.backgroundColor($r('app.color.app_background_color'))
.hideTitleBar(true)
// ...
}
} export default class EntryAbility extends UIAbility {
onCreate(_want: Want, _launchParam: AbilityConstant.LaunchParam): void {
AppStorage.setOrCreate<ConfigurationConstant.ColorMode>('currentColorMode', this.context.config.colorMode);
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
}
// ...
onConfigurationUpdate(newConfig: Configuration): void {
const currentColorMode: ConfigurationConstant.ColorMode | undefined = AppStorage.get('currentColorMode');
if (currentColorMode !== newConfig.colorMode) {
AppStorage.setOrCreate<ConfigurationConstant.ColorMode>('currentColorMode', newConfig.colorMode);
}
}
} @Entry
@Component
struct Index {
// ...
@StorageProp('currentColorMode') @Watch('onCurrentColorModeChange') currentColorMode: ConfigurationConstant.ColorMode =
ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET;
private windowObj: window.Window | null = null;
aboutToAppear(): void {
window.getLastWindow(this.getUIContext().getHostContext(), (err: BusinessError, data) => {
if (err.code) {
hilog.error(0x0000, 'Index', `getLastWindow failed. code=${err.code}, message=${err.message}`);
return;
}
this.windowObj = data;
})
}
onCurrentColorModeChange(): void {
if (!this.windowObj) {
return;
}
try {
if (this.currentColorMode === ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT) {
this.windowObj?.setWindowSystemBarProperties({
statusBarContentColor: '#000000'
})
} else if (this.currentColorMode === ConfigurationConstant.ColorMode.COLOR_MODE_DARK) {
this.windowObj?.setWindowSystemBarProperties({
statusBarContentColor: '#FFFFFF'
})
}
} catch (error) {
let err = error as BusinessError;
hilog.error(0x0000, 'Index', `setWindowSystemBarProperties failed, error code=${err.code}, message=${err.message}`);
}
}
// ...
build() {
// ...
}
} 
The content on the web page does not automatically switch with the system color mode.
If the web page needs to be adapted to dark and light color modes, you need to set the page style in dark mode through media query on the web page and use the darkMode() attribute of the Web component to control whether to enable the dark mode on the web page.
For details about the implementation, see Setting Dark Mode.
Possible causes
If no background color is set for the custom dialog box, the system adapts the default background color of the dialog box to the dark mode. However, the content in the dialog box, especially the custom content, cannot be automatically adapted to the dark mode. As a result, the dialog box does not change with the system color mode.
Solution
Adapt the dialog box content by referring to the above adaptation process.
Symptom
When you adapt a media resource (such as an image or icon), a dialog box is displayed, indicating that the resource already exists in another directory.
Solution
Click Continue in the dialog box. This will not result in adaptation errors.