The Toggle component provides checkbox, button, and switch styles, and is suitable for scenarios that require quick state switching or single-choice confirmation, effectively improving the interaction experience and UI intuitiveness.
This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
Since API version 26.0.0, the Toggle component supports the system material effect. When the Toggle component uses the universal system material attribute systemMaterial, the effect varies with the ToggleType type:
Toggle(options: ToggleOptions)
Widget capability: This API can be used in ArkTS widgets since API version 9.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.ArkUI.ArkUI.Full
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| options | ToggleOptions | Yes | Configuration options of the Toggle component, used to configure the style type and initial state of the switch. |
Provides the configuration options of the Toggle component.
To standardize anonymous object definitions, the element definitions here have been revised in API version 18. While historical version information is preserved for anonymous objects, there may be cases where the outer element's @since version number is larger than inner elements'. This does not affect interface usability.
Widget capability: This API can be used in ArkTS widgets since API version 18.
Atomic service API: This API can be used in atomic services since API version 18.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.ArkUI.ArkUI.Full
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| type8+ | ToggleType | No | No | Type of the toggle. Default value: ToggleType.Switch Widget capability: This API can be used in ArkTS widgets since API version 9. Atomic service API: This API can be used in atomic services since API version 11. |
| isOn8+ | boolean | No | Yes | Whether the toggle is turned on. true: on. false: off. Default value: false This parameter supports two-way binding through $$. This property supports two-way binding through !!. Widget capability: This API can be used in ArkTS widgets since API version 9. Atomic service API: This API can be used in atomic services since API version 11. |
Enumerates toggle types.
The toggle type inherits the default value of the corresponding component style and cannot be set. For example, if the value of ToggleType is Button, the component style inherits the default value of ButtonType. Since API version 18, the default type of Button.type has changed from capsule to rounded rectangle. Capsule buttons do not support setting borderRadius. In this case, setting borderRadius using the Toggle component also does not take effect.
Widget capability: This API can be used in ArkTS widgets since API version 9.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.ArkUI.ArkUI.Full
| Name | Value | Description |
|---|---|---|
| Checkbox | 0 | Checkbox type. Note: Since API version 11, the default style of the Checkbox component is changed from rounded square to circle. The default value of the universal attribute margin is as follows: { top: '14px', right: '14px', bottom: '14px', left: '14px' }. Default size: {width:'20vp', height:'20vp'}. |
| Switch | 1 | Switch type. Note: The default value of the universal attribute margin is as follows: { top: '6px', right: '14px', bottom: '6px', left: '14px' }. Default size: {width:'36vp', height:'20vp'}. |
| Button | 2 | Status button type. If child content contains text, the text is displayed on the button. The default height is 28 vp, and there is no default width. |
selectedColor(value: ResourceColor)
Sets the background color of the component when it is turned on.
For the impact on the background color when systemMaterial is set, see the description at the beginning of this topic.
Widget capability: This API can be used in ArkTS widgets since API version 9.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.ArkUI.ArkUI.Full
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| value | ResourceColor | Yes | Background color of the component when it is turned on. Default value: When ToggleType is set to Switch, the default value is $r('sys.color.ohos_id_color_emphasize'). When ToggleType is set to Checkbox, the default value is $r('sys.color.ohos_id_color_emphasize'). When ToggleType is set to Button, the default value is $r('sys.color.ohos_id_color_emphasize') with the opacity of $r('sys.float.ohos_id_alpha_highlight_bg'). |
switchPointColor(color: ResourceColor)
Sets the color of the circular slider when the component is of the Switch type. This attribute is valid only when type is set to ToggleType.Switch.
Widget capability: This API can be used in ArkTS widgets since API version 9.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.ArkUI.ArkUI.Full
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| color | ResourceColor | Yes | Color of the circular slider when the component is of the Switch type. Default value: $r('sys.color.ohos_id_color_foreground_contrary') Note: When systemMaterial is set simultaneously, the default white point light effect shows on the slider. After this attribute is set, the point light color follows the setting of this attribute. |
switchStyle(value: SwitchStyle)
Sets the style for the component of the Switch type. This attribute is valid only when type is set to ToggleType.Switch.
To set systemMaterial simultaneously, see the description at the beginning of this topic and the universal system material attribute document.
Atomic service API: This API can be used in atomic services since API version 12.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.ArkUI.ArkUI.Full
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| value | SwitchStyle | Yes | Style of the component of the Switch type. |
contentModifier(modifier: ContentModifier<ToggleConfiguration>)
Creates a content modifier.
Atomic service API: This API can be used in atomic services since API version 12.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.ArkUI.ArkUI.Full
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| modifier | ContentModifier<ToggleConfiguration> | Yes | Content modifier to apply to the Toggle component. modifier: content modifier. You need to customize a class to implement the ContentModifier API. |
Sets the style for the component of the Switch type.
To set systemMaterial simultaneously, see the description at the beginning of this topic and the universal system material attribute document.
Atomic service API: This API can be used in atomic services since API version 12.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.ArkUI.ArkUI.Full
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| pointRadius | number | Resource | No | Yes | Radius of the circular slider when the component is of the Switch type. The unit is vp. NOTE Percentage values are not supported. The value specified is used only when it is greater than or equal to 0. If the value is not specified or the specified one is less than 0, the radius is set using the following formula: (Component height (in vp)/2) - (2 vp x Component height (in vp)/20 vp) |
| unselectedColor | ResourceColor | No | Yes | Background color of the component when it is of the Switch type and is disabled. Default value: 0x337F7F7F (applies to both dark and light modes). Since API version 20, when optimizing color mode switching overhead is enabled, the default value is 0x19000000 (black with 10% opacity) in light mode and 0x19FFFFFF (white with 10% opacity) in dark mode. |
| pointColor | ResourceColor | No | Yes | Color of the circular slider when the component is of the Switch type. Default value: $r('sys.color.ohos_id_color_foreground_contrary') |
| trackBorderRadius | number | Resource | No | Yes | Radius of the slider track border corners when the component is of the Switch type. The unit is vp. NOTE This parameter cannot be set in percentage. If the value specified is less than 0, the radius is set using the default value formula. If the value specified is greater than half of the component height, the latter is used. In other cases, the value specified is used. If the value is not specified or the specified one is less than 0, the radius is set using the default value formula. Default value formula: Component height (in vp)/2 |
onChange(callback: (isOn: boolean) => void)
Triggered when the toggle status changes.
Widget capability: This API can be used in ArkTS widgets since API version 9.
Atomic service API: This API can be used in atomic services since API version 11.
System capability: SystemCapability.ArkUI.ArkUI.Full
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| isOn | boolean | Yes | Toggle status. true: The toggle is turned on. false: The toggle is turned off. |
You need a custom class to implement the ContentModifier API. This API inherits from CommonConfiguration.
Atomic service API: This API can be used in atomic services since API version 12.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.ArkUI.ArkUI.Full
| Name | Type | Read-Only | Optional | Description |
|---|---|---|---|---|
| isOn | boolean | No | No | Whether the toggle is turned on. true: The toggle is turned on. false: The toggle is turned off. Default value: false |
| enabled | boolean | No | No | Whether the toggle is enabled for state switching. true: The state can be changed. false: The state cannot be changed. Default value: true |
| triggerChange | Callback<boolean> | No | No | Callback used to trigger a change in the toggle's switch state. It is usually used in a custom ContentModifier to change the switch state programmatically. Invoking this callback with true sets the switch state to on, and calling it with false sets the switch state to off. |
This example demonstrates how to configure the style for different types of toggles (checkbox, switch, and button) using ToggleType.
// xxx.ets
@Entry
@Component
struct ToggleExample {
build() {
Column({ space: 10 }) {
Text('type: Switch').fontSize(12).fontColor(0xcccccc).width('90%')
Flex({ justifyContent: FlexAlign.SpaceEvenly, alignItems: ItemAlign.Center }) {
Toggle({ type: ToggleType.Switch, isOn: false })
.selectedColor('#007DFF')
.switchPointColor('#FFFFFF')
.onChange((isOn: boolean) => {
console.info('Component status:' + isOn);
})
Toggle({ type: ToggleType.Switch, isOn: true })
.selectedColor('#007DFF')
.switchPointColor('#FFFFFF')
.onChange((isOn: boolean) => {
console.info('Component status:' + isOn);
})
}
Text('type: Checkbox').fontSize(12).fontColor(0xcccccc).width('90%')
Flex({ justifyContent: FlexAlign.SpaceEvenly, alignItems: ItemAlign.Center }) {
Toggle({ type: ToggleType.Checkbox, isOn: false })
.size({ width: 20, height: 20 })
.selectedColor('#007DFF')
.onChange((isOn: boolean) => {
console.info('Component status:' + isOn);
})
Toggle({ type: ToggleType.Checkbox, isOn: true })
.size({ width: 20, height: 20 })
.selectedColor('#007DFF')
.onChange((isOn: boolean) => {
console.info('Component status:' + isOn);
})
}
Text('type: Button').fontSize(12).fontColor(0xcccccc).width('90%')
Flex({ justifyContent: FlexAlign.SpaceEvenly, alignItems: ItemAlign.Center }) {
Toggle({ type: ToggleType.Button, isOn: false }) {
Text('status button').fontColor('#182431').fontSize(12)
}.width(106)
.selectedColor('rgba(0,125,255,0.20)')
.onChange((isOn: boolean) => {
console.info('Component status:' + isOn);
})
Toggle({ type: ToggleType.Button, isOn: true }) {
Text('status button').fontColor('#182431').fontSize(12)
}.width(106)
.selectedColor('rgba(0,125,255,0.20)')
.onChange((isOn: boolean) => {
console.info('Component status:' + isOn);
})
}
}.width('100%').padding(24)
}
} 
This example implements a toggle of the Switch type with custom settings, including the radius and color of the circular slider, background color in the off state, and radius of the slider track border corners.
// xxx.ets
@Entry
@Component
struct ToggleExample {
build() {
Column({ space: 10 }) {
Text('type: Switch').fontSize(12).fontColor(0xcccccc).width('90%')
Flex({ justifyContent: FlexAlign.SpaceEvenly, alignItems: ItemAlign.Center }) {
Toggle({ type: ToggleType.Switch, isOn: false })
.selectedColor('#007DFF')
.switchStyle({
pointRadius: 15,
trackBorderRadius: 10,
pointColor: '#D2B48C',
unselectedColor: Color.Pink })
.onChange((isOn: boolean) => {
console.info('Component status:' + isOn);
})
Toggle({ type: ToggleType.Switch, isOn: true })
.selectedColor('#007DFF')
.switchStyle({
pointRadius: 15,
trackBorderRadius: 10,
pointColor: '#D2B48C',
unselectedColor: Color.Pink })
.onChange((isOn: boolean) => {
console.info('Component status:' + isOn);
})
}
}.width('100%').padding(24)
}
} 
This example shows how to implement a custom toggle style. The toggle button switches the background color. Clicking the blue circle changes the background to blue. Clicking the yellow circle changes it to yellow.
// xxx.ets
// Custom switch style modifier that implements the ContentModifier API to customize the toggle content area.
class MySwitchStyle implements ContentModifier<ToggleConfiguration> {
// Background color when the switch is on.
selectedColor: Color = Color.White;
// Text displayed on the button.
lamp: string = 'string';
constructor(selectedColor: Color, lamp: string) {
this.selectedColor = selectedColor;
this.lamp = lamp;
}
applyContent(): WrappedBuilder<[ToggleConfiguration]> {
return wrapBuilder(buildSwitch);
}
}
@Builder
function buildSwitch(config: ToggleConfiguration) {
Column({ space: 50 }) {
Circle({ width: 150, height: 150 })
.fill(config.isOn ? (config.contentModifier as MySwitchStyle).selectedColor : Color.Blue)
Row() {
Button('Blue ' + JSON.stringify((config.contentModifier as MySwitchStyle).lamp))
.onClick(() => {
config.triggerChange(false);
})
Button('Yellow ' + JSON.stringify((config.contentModifier as MySwitchStyle).lamp))
.onClick(() => {
config.triggerChange(true);
})
}
}
}
@Entry
@Component
struct Index {
build() {
Column({ space: 50 }) {
// Use the custom style modifier to customize the toggle content, and listen for state changes through onChange.
Toggle({ type: ToggleType.Switch })
.enabled(true)
.contentModifier(new MySwitchStyle(Color.Yellow, 'light'))
.onChange((isOn: boolean) => {
console.info('Switch Log:' + isOn);
})
}.height('100%').width('100%')
}
} 
This example shows the effect comparison of the Toggle component of the Switch type before and after the immersive light effect is enabled. In this example, you can use the universal attribute systemMaterial to implement the immersive light effect.
The illustration in this example shows the strong effect on a high-computing-power device. The immersive light effect of the component is adaptively adjusted based on the device computing power and the immersive light effect set by the user in the system, and you do not need to perform additional adaptation.
Since API version 26.0.0, the systemMaterial attribute is added.
The actual display effect of the system material is related to the device computing power. The same code produces different display effects on devices of different computing power levels, and a simplified material effect is displayed on low-computing-power devices. The computing power levels are automatically divided and managed by the system based on the hardware capabilities of the device. Applications do not need to be aware of them or perform additional configuration. The system automatically adapts the material display effect based on the computing power level of the current device.
import { uiMaterial } from '@kit.ArkUI';
// xxx.ets
@Entry
@Component
struct ToggleMaterialTest {
build() {
Stack() {
// Set the system material to enable the immersive light effect (the systemMaterial parameter is arbitrary and serves only as the system material switch; the component-side fixed parameters are ultimately used), with a white point light by default (the color is the default value of switchPointColor).
Toggle({ type: ToggleType.Switch, isOn: true })
.systemMaterial(new uiMaterial.Material())
}
.width('100%')
.height('100%')
}
} System material not set

System material set
