1e41f4b71Sopenharmony_ci# Media Query (@ohos.mediaquery) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ci## Overview 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci[Media queries](../reference/apis-arkui/js-apis-mediaquery.md), the cornerstone of responsive design, are widely used on mobile devices. You can use media queries to apply application styles based on the device type or device state. Specifically, media queries allow you to: 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci1. Design a layout style based on the device and application attributes (such as display area, dark light color, and resolution). 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci2. Update the page layout to adapt to dynamic screen changes (for example, screen splitting or switching between landscape and portrait modes). 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci## Usage 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ciInvoke the API in the **mediaquery** module to set the media query condition and the callback, and change the page layout or implement service logic in the callback corresponding to the condition. The procedure is as follows: 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ciImport the **mediaquery** module, as shown below: 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci```ts 21e41f4b71Sopenharmony_ciimport { mediaquery } from '@kit.ArkUI'; 22e41f4b71Sopenharmony_ci``` 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ciUse the **matchMediaSync** API to set the media query condition and save the returned listener. The following is the example for listening for landscape events: 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci```ts 28e41f4b71Sopenharmony_cilet listener: mediaquery.MediaQueryListener = mediaquery.matchMediaSync('(orientation: landscape)'); 29e41f4b71Sopenharmony_ci``` 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ciRegister the **onPortrait** callback using the saved listener, and change the page layout or implement service logic in the callback. When the media query condition is matched, the callback is triggered. The sample code is as follows: 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci```ts 35e41f4b71Sopenharmony_cionPortrait(mediaQueryResult: mediaquery.MediaQueryResult) { 36e41f4b71Sopenharmony_ci if (mediaQueryResult.matches as boolean) { 37e41f4b71Sopenharmony_ci // do something here 38e41f4b71Sopenharmony_ci } else { 39e41f4b71Sopenharmony_ci // do something here 40e41f4b71Sopenharmony_ci } 41e41f4b71Sopenharmony_ci} 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_cilistener.on('change', onPortrait); 44e41f4b71Sopenharmony_ci``` 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci## Media Query Conditions 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ciThe media query condition consists of the media type (optional), logical operator, and media feature. The logical operator is used to connect different media types and media features. A media feature must be enclosed in parentheses (). There may be multiple media features. 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci### Syntax 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ciSyntax rules include [media-type](#media-type), [media-logic-operations](#media-logic-operations), and [media-feature](#media-feature). 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci```ts 58e41f4b71Sopenharmony_ci[media-type] [media-logic-operations] [(media-feature)] 59e41f4b71Sopenharmony_ci``` 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ciExamples are as follows: 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci- **screen and (round-screen: true)**: The query is valid when the device screen is round. 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci- **(max-height: 800px)**: The query is valid when the height is less than or equal to 800 px. 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci- **(height <= 800px)**: The query is valid when the height is less than or equal to 800 px. 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci- **screen and (device-type: tv) or (resolution < 2)**: The query is valid when the device type is TV or the device resolution is less than 2. This is a multi-condition query that contains multiple media features. 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci- **(dark-mode: true)**: The query is valid when the system is in dark mode. 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci### media-type 75e41f4b71Sopenharmony_ciWhen media types are not specified in a media query, the default type **screen** is used. Media types must be placed at the beginning of the query condition. 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci| **Type**| **Description** | 78e41f4b71Sopenharmony_ci| ------ | -------------- | 79e41f4b71Sopenharmony_ci| screen | Media query based on screen-related parameters.| 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci### media-logic-operations 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ciYou can use logical operators (**and**, **or**, **not**, and **only**) to compose complex media queries. You can also combine them using comma (,). The following table describes the operators. 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci **Table 1** Media logical operators 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ci| Type | Description | 89e41f4b71Sopenharmony_ci| ---------------- | ------------------------------------------------------------ | 90e41f4b71Sopenharmony_ci| and | The **and** operator is used to combine multiple media features into one media query, in a logical AND operation. The query is valid only when all media features are true. It can also combine media types and media functions. For example, **screen and (device-type: wearable) and (max-height: 600px)** evaluates to **true** when the device type is wearable and the maximum height of the application is 600 pixel units.| 91e41f4b71Sopenharmony_ci| or | The **or** operator is used to combine multiple media features into one media query, in a logical OR operation. The query is valid if a media feature is true. For example, **screen and (max-height: 1000px) or (round-screen: true)** indicates that the query is valid when the maximum height of the application is 1000 pixel units or the device screen is round.| 92e41f4b71Sopenharmony_ci| not | The **not** operator is used to perform a logical negation for a media query. It must be used in conjunction with **screen**. **true** is returned if the query condition is not met. Otherwise, **false** is returned. For example, **not screen and (min-height: 50px) and (max-height: 600px)** evaluates to **true** when the height of the application is less than 50 pixel units or greater than 600 pixel units.| 93e41f4b71Sopenharmony_ci| only | The **only** operator must be used with **screen** to ensure that the styles are only applied when the media query matches. It has the same effect as using **screen** alone. Example: **only screen and (height <= 50) **| 94e41f4b71Sopenharmony_ci| comma (,) | The **or** operator is used to combine multiple media features into one media query, in a logical OR operation. The query is valid if a media feature is true. The effect of a comma operator is equivalent to that of the **or** operator. For example, **screen and (min-height: 1000px), (round-screen: true)** indicates that the query is valid when the minimum height of the application is 1000 pixel units or the device screen is round.| 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ciMedia range operators include <=, >=, <, and >. For details, see the following table. 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci **Table 2** Logical operators for range query 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci| Type | Description | 101e41f4b71Sopenharmony_ci| ----- | ---------------------------------------- | 102e41f4b71Sopenharmony_ci| <= | Less than or equal to, for example, **screen and (50 <= height)**.| 103e41f4b71Sopenharmony_ci| >= | Greater than or equal to, for example, **screen and (600 >= height)**.| 104e41f4b71Sopenharmony_ci| < | Less than, for example, **screen and (50 < height)**.| 105e41f4b71Sopenharmony_ci| > | Greater than, for example, **screen and (height > 600)**.| 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ci### media-feature 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ciThe media features include the width and height of the application display area, device resolution, and device width and height. For details, see the following table. 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci **Table 3** Media features 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ciFor width and height related features, the units vp and px are supported. If no unit is specified, px is used by default. 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci| Type | Description | 117e41f4b71Sopenharmony_ci| ----------------- | ---------------------------------------- | 118e41f4b71Sopenharmony_ci| height | Height of the drawing area of the application. | 119e41f4b71Sopenharmony_ci| min-height | Minimum height of the drawing area of the application. | 120e41f4b71Sopenharmony_ci| max-height | Maximum height of the drawing area of the application. | 121e41f4b71Sopenharmony_ci| width | Width of the drawing area of the application. | 122e41f4b71Sopenharmony_ci| min-width | Minimum width of the drawing area of the application. | 123e41f4b71Sopenharmony_ci| max-width | Maximum width of the drawing area of the application. | 124e41f4b71Sopenharmony_ci| resolution | Resolution of the device. The unit can be dpi, dppx, or dpcm. <br>- **dpi** indicates the number of physical pixels per inch. 1 dpi ≈ 0.39 dpcm.<br>- **dpcm** indicates the number of physical pixels per centimeter. 1 dpcm ≈ 2.54 dpi.<br>- **dppx** indicates the number of physical pixels in each pixel. (This unit is calculated based on this formula: 96 px = 1 inch, which is different from the calculation method of the px unit on the page.) 1 dppx = 96 dpi.| 125e41f4b71Sopenharmony_ci| min-resolution | Minimum device resolution. | 126e41f4b71Sopenharmony_ci| max-resolution | Maximum device resolution. | 127e41f4b71Sopenharmony_ci| orientation | Screen orientation.<br>Options are as follows:<br>- orientation: portrait<br>- orientation: landscape| 128e41f4b71Sopenharmony_ci| device-height | Height of the device. | 129e41f4b71Sopenharmony_ci| min-device-height | Minimum height of the device. | 130e41f4b71Sopenharmony_ci| max-device-height | Maximum height of the device. | 131e41f4b71Sopenharmony_ci| device-width | Width of the device. The settings are only saved once during application initialization and do not update in real-time with device width changes, such as in scenarios where a foldable screen is folded or unfolded. | 132e41f4b71Sopenharmony_ci| device-type | Type of the device.<br>Available options: **default** and **tablet** | 133e41f4b71Sopenharmony_ci| min-device-width | Minimum width of the device. | 134e41f4b71Sopenharmony_ci| max-device-width | Maximum width of the device. | 135e41f4b71Sopenharmony_ci| round-screen | Screen type. The value **true** indicates a circular screen, and **false** indicates a non-circular screen. | 136e41f4b71Sopenharmony_ci| dark-mode | Whether the device is in dark mode. The value **true** means that the device is in dark mode, and **false** means the opposite. | 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ci>**NOTE** 139e41f4b71Sopenharmony_ci> 140e41f4b71Sopenharmony_ci>Of the preceding media features, only height and width are supported on widgets. 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci## Example Scenario 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ciIn the following examples, media queries are used to apply different content and styles to the page text when the screen is switched between landscape and portrait modes. 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ciStage model: 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ci```ts 150e41f4b71Sopenharmony_ciimport { mediaquery, window } from '@kit.ArkUI'; 151e41f4b71Sopenharmony_ciimport { common } from '@kit.AbilityKit'; 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci@Entry 154e41f4b71Sopenharmony_ci@Component 155e41f4b71Sopenharmony_cistruct MediaQueryExample { 156e41f4b71Sopenharmony_ci @State color: string = '#DB7093'; 157e41f4b71Sopenharmony_ci @State text: string = 'Portrait'; 158e41f4b71Sopenharmony_ci @State portraitFunc:mediaquery.MediaQueryResult|void|null = null; 159e41f4b71Sopenharmony_ci // The query is valid when the device is in landscape mode. 160e41f4b71Sopenharmony_ci listener:mediaquery.MediaQueryListener = this.getUIContext().getMediaQuery().matchMediaSync('(orientation: landscape)'); 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ci // The callback is triggered when the query is valid. 163e41f4b71Sopenharmony_ci onPortrait(mediaQueryResult:mediaquery.MediaQueryResult) { 164e41f4b71Sopenharmony_ci if (mediaQueryResult.matches as boolean) {// If the device is in landscape mode, the page layout is changed accordingly. 165e41f4b71Sopenharmony_ci this.color = '#FFD700'; 166e41f4b71Sopenharmony_ci this.text = 'Landscape'; 167e41f4b71Sopenharmony_ci } else { 168e41f4b71Sopenharmony_ci this.color = '#DB7093'; 169e41f4b71Sopenharmony_ci this.text = 'Portrait'; 170e41f4b71Sopenharmony_ci } 171e41f4b71Sopenharmony_ci } 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci aboutToAppear() { 174e41f4b71Sopenharmony_ci // Bind to the current application instance. 175e41f4b71Sopenharmony_ci // Register the callback. 176e41f4b71Sopenharmony_ci this.listener.on('change', (mediaQueryResult: mediaquery.MediaQueryResult) => { 177e41f4b71Sopenharmony_ci this.onPortrait(mediaQueryResult) 178e41f4b71Sopenharmony_ci }); 179e41f4b71Sopenharmony_ci } 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci aboutToDisappear() { 182e41f4b71Sopenharmony_ci // Unbind the callback function registered in the listener. 183e41f4b71Sopenharmony_ci this.listener.off('change'); 184e41f4b71Sopenharmony_ci } 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci // Change the landscape/portrait mode of the device in the callback. 187e41f4b71Sopenharmony_ci private changeOrientation(isLandscape: boolean) { 188e41f4b71Sopenharmony_ci // Obtain the context information of the UIAbility instance. 189e41f4b71Sopenharmony_ci let context:common.UIAbilityContext = getContext(this) as common.UIAbilityContext; 190e41f4b71Sopenharmony_ci // Invoke this API to manually change the landscape/portrait mode of the device. 191e41f4b71Sopenharmony_ci window.getLastWindow(context).then((lastWindow) => { 192e41f4b71Sopenharmony_ci lastWindow.setPreferredOrientation(isLandscape ? window.Orientation.LANDSCAPE : window.Orientation.PORTRAIT) 193e41f4b71Sopenharmony_ci }); 194e41f4b71Sopenharmony_ci } 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ci build() { 197e41f4b71Sopenharmony_ci Column({ space: 50 }) { 198e41f4b71Sopenharmony_ci Text(this.text).fontSize(50).fontColor(this.color) 199e41f4b71Sopenharmony_ci Text('Landscape').fontSize(50).fontColor(this.color).backgroundColor(Color.Orange) 200e41f4b71Sopenharmony_ci .onClick(() => { 201e41f4b71Sopenharmony_ci this.changeOrientation(true); 202e41f4b71Sopenharmony_ci }) 203e41f4b71Sopenharmony_ci Text('Portrait').fontSize(50).fontColor(this.color).backgroundColor(Color.Orange) 204e41f4b71Sopenharmony_ci .onClick(() => { 205e41f4b71Sopenharmony_ci this.changeOrientation(false); 206e41f4b71Sopenharmony_ci }) 207e41f4b71Sopenharmony_ci } 208e41f4b71Sopenharmony_ci .width('100%').height('100%') 209e41f4b71Sopenharmony_ci } 210e41f4b71Sopenharmony_ci} 211e41f4b71Sopenharmony_ci``` 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ciFA model: 214e41f4b71Sopenharmony_ci 215e41f4b71Sopenharmony_ci 216e41f4b71Sopenharmony_ci```ts 217e41f4b71Sopenharmony_ciimport { mediaquery } from '@kit.ArkUI'; 218e41f4b71Sopenharmony_ciimport { featureAbility } from '@kit.AbilityKit'; 219e41f4b71Sopenharmony_ci 220e41f4b71Sopenharmony_ci@Entry 221e41f4b71Sopenharmony_ci@Component 222e41f4b71Sopenharmony_cistruct MediaQueryExample { 223e41f4b71Sopenharmony_ci @State color: string = '#DB7093'; 224e41f4b71Sopenharmony_ci @State text: string = 'Portrait'; 225e41f4b71Sopenharmony_ci @State portraitFunc:mediaquery.MediaQueryResult|void|null = null; 226e41f4b71Sopenharmony_ci listener:mediaquery.MediaQueryListener = mediaquery.matchMediaSync('(orientation: landscape)'); // The query is valid when the device is in landscape mode. 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_ci onPortrait(mediaQueryResult:mediaquery.MediaQueryResult) { // The callback is triggered when the query is valid. 229e41f4b71Sopenharmony_ci if (mediaQueryResult.matches as boolean) {// If the device is in landscape mode, the page layout is changed accordingly. 230e41f4b71Sopenharmony_ci this.color = '#FFD700'; 231e41f4b71Sopenharmony_ci this.text = 'Landscape'; 232e41f4b71Sopenharmony_ci } else { 233e41f4b71Sopenharmony_ci this.color = '#DB7093'; 234e41f4b71Sopenharmony_ci this.text = 'Portrait'; 235e41f4b71Sopenharmony_ci } 236e41f4b71Sopenharmony_ci } 237e41f4b71Sopenharmony_ci 238e41f4b71Sopenharmony_ci aboutToAppear() { 239e41f4b71Sopenharmony_ci // Bind to the current application instance. 240e41f4b71Sopenharmony_ci this.listener.on('change', (mediaQueryResult:mediaquery.MediaQueryResult) => { this.onPortrait(mediaQueryResult) }); // Register the callback. 241e41f4b71Sopenharmony_ci } 242e41f4b71Sopenharmony_ci 243e41f4b71Sopenharmony_ci aboutToDisappear() { 244e41f4b71Sopenharmony_ci // Unbind the callback function registered in the listener. 245e41f4b71Sopenharmony_ci this.listener.off('change'); 246e41f4b71Sopenharmony_ci } 247e41f4b71Sopenharmony_ci 248e41f4b71Sopenharmony_ci build() { 249e41f4b71Sopenharmony_ci Column({ space: 50 }) { 250e41f4b71Sopenharmony_ci Text(this.text).fontSize(50).fontColor(this.color) 251e41f4b71Sopenharmony_ci Text('Landscape').fontSize(50).fontColor(this.color).backgroundColor(Color.Orange) 252e41f4b71Sopenharmony_ci .onClick(() => { 253e41f4b71Sopenharmony_ci let context = featureAbility.getContext(); 254e41f4b71Sopenharmony_ci context.setDisplayOrientation(0); // Invoke this API to manually change the landscape/portrait mode of the device. 255e41f4b71Sopenharmony_ci }) 256e41f4b71Sopenharmony_ci Text('Portrait').fontSize(50).fontColor(this.color).backgroundColor(Color.Orange) 257e41f4b71Sopenharmony_ci .onClick(() => { 258e41f4b71Sopenharmony_ci let context = featureAbility.getContext(); 259e41f4b71Sopenharmony_ci context.setDisplayOrientation(1); // Invoke this API to manually change the landscape/portrait mode of the device. 260e41f4b71Sopenharmony_ci }) 261e41f4b71Sopenharmony_ci } 262e41f4b71Sopenharmony_ci .width('100%').height('100%') 263e41f4b71Sopenharmony_ci } 264e41f4b71Sopenharmony_ci} 265e41f4b71Sopenharmony_ci``` 266e41f4b71Sopenharmony_ci 267e41f4b71Sopenharmony_ci **Figure 1** Portrait mode 268e41f4b71Sopenharmony_ci 269e41f4b71Sopenharmony_ci 270e41f4b71Sopenharmony_ci 271e41f4b71Sopenharmony_ci **Figure 2** Landscape mode 272e41f4b71Sopenharmony_ci 273e41f4b71Sopenharmony_ci 274e41f4b71Sopenharmony_ci 275