Switch (lv_switch)
A small slider that displays and toggles an on/off value.
Overview
The Switch looks like a small slider and displays — and optionally toggles — an on/off value. It is horizontal by default and becomes vertical when its width is smaller than its height.
Learn more in the LVGL Open documentation.
/**
* @file lv_example_switch.c
*/
#include "../lv_examples.h"
/**
* @title Switch
* @brief A checked and a disabled switch, plus a two-way bound pair.
*
* The first switch starts `checked="true"`, the second is `disabled="true"`.
* The third uses `bind_checked="subject_checked"`; the fourth binds to the
* same subject, so toggling either one moves both — the classic two-way
* binding pattern.
*/
void lv_example_switch(void)
{
static lv_style_t style_sw;
static lv_style_t style_sw_on;
static lv_style_t style_knob;
static lv_subject_t subject_checked;
static bool inited = false;
if (!inited) {
/*Init all styles*/
lv_style_init(&style_sw);
lv_style_init(&style_sw_on);
lv_style_init(&style_knob);
lv_style_set_bg_color(&style_sw, lv_color_hex(0xcbd5e1));
lv_style_set_bg_opa(&style_sw, (255 * 100 / 100));
lv_style_set_bg_color(&style_sw_on, lv_color_hex(0x16a34a));
lv_style_set_bg_color(&style_knob, lv_color_hex(0xffffff));
lv_subject_init_int(&subject_checked, 0);
inited = true;
}
lv_obj_t * screen = lv_screen_active();
lv_obj_set_flex_flow(screen, LV_FLEX_FLOW_COLUMN);
lv_obj_set_style_pad_row(screen, 16, 0);
lv_obj_set_style_pad_all(screen, 18, 0);
lv_obj_set_style_flex_cross_place(screen, LV_FLEX_ALIGN_CENTER, 0);
lv_obj_set_style_flex_track_place(screen, LV_FLEX_ALIGN_CENTER, 0);
lv_obj_set_style_flex_main_place(screen, LV_FLEX_ALIGN_CENTER, 0);
/* On by default */
lv_obj_t * lv_switch_0 = lv_switch_create(screen);
lv_obj_set_state(lv_switch_0, LV_STATE_CHECKED, true);
lv_obj_add_style(lv_switch_0, &style_sw, 0);
lv_obj_add_style(lv_switch_0, &style_sw_on, LV_PART_INDICATOR | LV_STATE_CHECKED);
lv_obj_add_style(lv_switch_0, &style_knob, LV_PART_KNOB);
/* Disabled */
lv_obj_t * lv_switch_1 = lv_switch_create(screen);
lv_obj_set_state(lv_switch_1, LV_STATE_DISABLED, true);
lv_obj_set_style_bg_opa(lv_switch_1, (255 * 40 / 100), 0);
lv_obj_add_style(lv_switch_1, &style_sw, 0);
lv_obj_add_style(lv_switch_1, &style_knob, LV_PART_KNOB);
/* Two switches bound to the same subject */
lv_obj_t * lv_switch_2 = lv_switch_create(screen);
lv_obj_bind_checked(lv_switch_2, &subject_checked);
lv_obj_add_style(lv_switch_2, &style_sw, 0);
lv_obj_add_style(lv_switch_2, &style_sw_on, LV_PART_INDICATOR | LV_STATE_CHECKED);
lv_obj_add_style(lv_switch_2, &style_knob, LV_PART_KNOB);
lv_obj_t * lv_switch_3 = lv_switch_create(screen);
lv_obj_bind_checked(lv_switch_3, &subject_checked);
lv_obj_add_style(lv_switch_3, &style_sw, 0);
lv_obj_add_style(lv_switch_3, &style_sw_on, LV_PART_INDICATOR | LV_STATE_CHECKED);
lv_obj_add_style(lv_switch_3, &style_knob, LV_PART_KNOB);
}
<!--
@title Switch
@brief A checked and a disabled switch, plus a two-way bound pair.
The first switch starts `checked="true"`, the second is `disabled="true"`.
The third uses `bind_checked="subject_checked"`; the fourth binds to the
same subject, so toggling either one moves both — the classic two-way
binding pattern.
-->
<screen>
<styles>
<style name="style_sw" bg_color="0xcbd5e1" bg_opa="100%" />
<style name="style_sw_on" bg_color="0x16a34a" />
<style name="style_knob" bg_color="0xffffff" />
</styles>
<view
flex_flow="column"
style_pad_row="16"
style_pad_all="18"
style_flex_cross_place="center"
style_flex_track_place="center"
style_flex_main_place="center"
>
<!-- On by default -->
<lv_switch checked="true">
<style name="style_sw" />
<style name="style_sw_on" selector="checked|indicator" />
<style name="style_knob" selector="knob" />
</lv_switch>
<!-- Disabled -->
<lv_switch disabled="true" style_bg_opa="40%">
<style name="style_sw" />
<style name="style_knob" selector="knob" />
</lv_switch>
<!-- Two switches bound to the same subject -->
<lv_switch bind_checked="subject_checked">
<style name="style_sw" />
<style name="style_sw_on" selector="checked|indicator" />
<style name="style_knob" selector="knob" />
</lv_switch>
<lv_switch bind_checked="subject_checked">
<style name="style_sw" />
<style name="style_sw_on" selector="checked|indicator" />
<style name="style_knob" selector="knob" />
</lv_switch>
</view>
</screen>
Enumerations
lv_switch_orientation — Switch orientation
| Value | Description |
|---|---|
auto | Choose based on widget size |
horizontal | Horizontal switch |
vertical | Vertical switch |
Properties
Parts
Style parts of the widget with local style (e.g. style_bg_color-knob="0xff0000") or with style sheets (e.g. <style name="style_knob" selector="knob">). See Styles to learn more.
| Part | Description |
|---|---|
main | The background track: background properties. padding shrinks the indicator. |
indicator | The fill that shows the on/off state: background properties. Use the 'checked' state (e.g. indicator|checked) for the ON color. |
knob | The sliding handle: background, border, shadow and padding properties. |
Properties below are the widget's XML <api>; see API for how properties, parameters and elements work.
| Property | Type | Description |
|---|---|---|
orientation | enum:lv_switch_orientation | Set switch orientation |
Last updated on