Switch
Switch is an animated on/off toggle that looks like a sliding pill. When toggled, the inner indicator slides from one end to the other with a smooth CSS transition. It has no text label — use a Label or Toggle if you need descriptive text.
Internally, Switch is a horizontal flex row containing a flex spacer (placeholder) and a square indicator (markIcon). The spacer grows from flex: 0 to flex: 1 on toggle, pushing the indicator to the far side.
Everything documented on UIElement (layout, styles, events, data bindings, etc.) applies here too.
Usage
XML
| XML Attribute | Type | Description |
|---|---|---|
is-on |
boolean |
Initial on/off state. Default: false. |
Internal Structure
Switch contains two internal elements:
| Index | Field | Type | CSS class | Description |
|---|---|---|---|---|
0 |
placeholder |
UIElement |
— | Flex spacer that grows/shrinks to animate the indicator. |
1 |
markIcon |
UIElement |
.__switch_mark-icon__ |
The sliding square indicator. |
Switch Style
SwitchStyle controls four textures: the container background in each state and the indicator texture in each state.
base-background
Container background when the switch is off.
Default: Sprites.RECT_RD_DARK
pressed-background
Container background when the switch is on.
Default: Sprites.RECT_RD_T
unmark-background
Texture of the sliding indicator when the switch is off.
Default: Sprites.RECT_RD
mark-background
Texture of the sliding indicator when the switch is on.
Default: Sprites.RECT_RD
Value Binding
Switch extends BindableUIElement<Boolean>, so it integrates with the data-binding system:
See Data Bindings for full details.
Fields
| Name | Type | Access | Description |
|---|---|---|---|
placeholder |
UIElement |
public final |
The flex spacer that drives the slide animation. |
markIcon |
UIElement |
public final |
The visible sliding indicator. |
switchStyle |
SwitchStyle |
private (getter) |
Current switch style. |
isOn |
boolean |
private (getter) |
Current on/off state. |
Methods
| Method | Returns | Description |
|---|---|---|
setOn(boolean) |
Switch |
Sets the on/off state and notifies listeners. |
setOnSwitchChanged(BooleanConsumer) |
Switch |
Registers a listener for state changes. |
switchStyle(Consumer<SwitchStyle>) |
Switch |
Configures SwitchStyle fluently. |
getValue() |
Boolean |
Returns the current on/off state. |