1e41f4b71Sopenharmony_ci# @ohos.mediaquery (Media Query) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **mediaquery** module provides different styles for different media types. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version. 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> This module cannot be used in the file declaration of the [UIAbility](../apis-ability-kit/js-apis-app-ability-uiAbility.md). In other words, the APIs of this module can be used only after a component instance is created; they cannot be called in the lifecycle of the UIAbility. 10e41f4b71Sopenharmony_ci> 11e41f4b71Sopenharmony_ci> The functionality of this module depends on UI context. This means that the APIs of this module cannot be used where the UI context is unclear. For details, see [UIContext](js-apis-arkui-UIContext.md#uicontext). 12e41f4b71Sopenharmony_ci> 13e41f4b71Sopenharmony_ci> Since API version 10, you can use the [getMediaQuery](js-apis-arkui-UIContext.md#getmediaquery) API in [UIContext](js-apis-arkui-UIContext.md#uicontext) to obtain the [MediaQuery](js-apis-arkui-UIContext.md#mediaquery) object associated with the current UI context. 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci## Modules to Import 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci```ts 19e41f4b71Sopenharmony_ciimport { mediaquery } from '@kit.ArkUI'; 20e41f4b71Sopenharmony_ci``` 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci## mediaquery.matchMediaSync 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_cimatchMediaSync(condition: string): MediaQueryListener 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ciSets the media query condition. This API returns the corresponding media query listener. 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci**Widget capability**: This API can be used in ArkTS widgets since API version 12. 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci**Parameters** 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 38e41f4b71Sopenharmony_ci| --------- | ------ | ---- | ------------------------------------------------------------ | 39e41f4b71Sopenharmony_ci| condition | string | Yes | Media query condition. For details, see [Syntax](../../ui/arkts-layout-development-media-query.md#syntax). | 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci**Return value** 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci| Type | Description | 44e41f4b71Sopenharmony_ci| ------------------ | -------------------------------------------- | 45e41f4b71Sopenharmony_ci| [MediaQueryListener](#mediaquerylistener) | Media query listener, which is used to register or deregister the listening callback. | 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci**Example** 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci```ts 50e41f4b71Sopenharmony_ciimport { mediaquery } from '@kit.ArkUI'; 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_cilet listener:mediaquery.MediaQueryListener = mediaquery.matchMediaSync('(orientation: landscape)'); // Listen for landscape events. 53e41f4b71Sopenharmony_ci``` 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci## MediaQueryListener 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ciImplements the media query listener, including the first query result when the listener is applied for. The specified media query condition, for example, **'(width <= 600vp)'**, is compared system information. If related information is not initialized during the first query, **matches** returns **false**. 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ciInherits from [MediaQueryResult](#mediaqueryresult). 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci**Widget capability**: This API can be used in ArkTS widgets since API version 12. 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci### on('change') 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_cion(type: 'change', callback: Callback<MediaQueryResult>): void 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ciRegisters a media query listener. The callback is triggered when the media attributes change. 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci> **NOTE** 76e41f4b71Sopenharmony_ci> 77e41f4b71Sopenharmony_ci> The **on** or **off** function cannot be called in the registered callback. 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci**Widget capability**: This API can be used in ArkTS widgets since API version 12. 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci**Parameters** 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 88e41f4b71Sopenharmony_ci| -------- | ----------------------------------------------------- | ---- | ------------------------ | 89e41f4b71Sopenharmony_ci| type | string | Yes | Listener type. The value is fixed at **'change'**. | 90e41f4b71Sopenharmony_ci| callback | Callback<[MediaQueryResult](#mediaqueryresult)> | Yes | Callback registered with media query. | 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ci**Example** 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci See the example of [off](#offchange). 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci### off('change') 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_cioff(type: 'change', callback?: Callback<MediaQueryResult>): void 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ciDeregisters a media query listener, so that no callback is triggered when the media attributes change. 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci**Widget capability**: This API can be used in ArkTS widgets since API version 12. 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci**Parameters** 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 112e41f4b71Sopenharmony_ci| -------- | -------------------------------- | ---- | ---------------------------------------------------------- | 113e41f4b71Sopenharmony_ci| type | string | Yes | Listener type. The value is fixed at **'change'**. | 114e41f4b71Sopenharmony_ci| callback | Callback<[MediaQueryResult](#mediaqueryresult)> | No | Callback to be deregistered. If the default value is used, all callbacks of the handle are deregistered. | 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci**Example** 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci ```ts 119e41f4b71Sopenharmony_ciimport { mediaquery } from '@kit.ArkUI'; 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_cilet listener = mediaquery.matchMediaSync('(orientation: landscape)'); // Listen for landscape events. 122e41f4b71Sopenharmony_cifunction onPortrait(mediaQueryResult:mediaquery.MediaQueryResult) { 123e41f4b71Sopenharmony_ci if (mediaQueryResult.matches) { 124e41f4b71Sopenharmony_ci // do something here 125e41f4b71Sopenharmony_ci } else { 126e41f4b71Sopenharmony_ci // do something here 127e41f4b71Sopenharmony_ci } 128e41f4b71Sopenharmony_ci} 129e41f4b71Sopenharmony_cilistener.on('change', onPortrait) // Register the media query listener. 130e41f4b71Sopenharmony_cilistener.off('change', onPortrait) // Deregister the listener. 131e41f4b71Sopenharmony_ci ``` 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci## MediaQueryResult 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ciProvides the media query result. 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci**Widget capability**: This API can be used in ArkTS widgets since API version 12. 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 11. 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci**System capability**: SystemCapability.ArkUI.ArkUI.Full 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci### Attributes 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ci| Name | Type | Readable | Writable | Description | 147e41f4b71Sopenharmony_ci| ------- | ------- | ---- | ---- | -------------------- | 148e41f4b71Sopenharmony_ci| matches | boolean | Yes | No | Whether the media query condition is met. | 149e41f4b71Sopenharmony_ci| media | string | Yes | No | Media query condition. | 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci### Example 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci```ts 155e41f4b71Sopenharmony_ciimport { mediaquery } from '@kit.ArkUI'; 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci@Entry 158e41f4b71Sopenharmony_ci@Component 159e41f4b71Sopenharmony_cistruct MediaQueryExample { 160e41f4b71Sopenharmony_ci @State color: string = '#DB7093' 161e41f4b71Sopenharmony_ci @State text: string = 'Portrait' 162e41f4b71Sopenharmony_ci listener = mediaquery.matchMediaSync('(orientation: landscape)') 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_ci onPortrait(mediaQueryResult:mediaquery.MediaQueryResult) { 165e41f4b71Sopenharmony_ci if (mediaQueryResult.matches) { 166e41f4b71Sopenharmony_ci this.color = '#FFD700' 167e41f4b71Sopenharmony_ci this.text = 'Landscape' 168e41f4b71Sopenharmony_ci } else { 169e41f4b71Sopenharmony_ci this.color = '#DB7093' 170e41f4b71Sopenharmony_ci this.text = 'Portrait' 171e41f4b71Sopenharmony_ci } 172e41f4b71Sopenharmony_ci } 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci aboutToAppear() { 175e41f4b71Sopenharmony_ci let portraitFunc = (mediaQueryResult:mediaquery.MediaQueryResult):void=>this.onPortrait(mediaQueryResult) // bind current js instance 176e41f4b71Sopenharmony_ci this.listener.on('change', portraitFunc) 177e41f4b71Sopenharmony_ci } 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ci build() { 180e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 181e41f4b71Sopenharmony_ci Text(this.text).fontSize(24).fontColor(this.color) 182e41f4b71Sopenharmony_ci } 183e41f4b71Sopenharmony_ci .width('100%').height('100%') 184e41f4b71Sopenharmony_ci } 185e41f4b71Sopenharmony_ci} 186e41f4b71Sopenharmony_ci``` 187