Checkbox (lv_checkbox)
A tick box with a label that toggles when clicked.
Overview
The Checkbox combines a tick box with a label. Clicking it toggles the checked state. The text is set directly on the widget and the tick box reflects the checked and disabled states.
Learn more in the LVGL Open documentation.
/**
* @file lv_example_checkbox.c
*/
#include "../lv_examples.h"
/**
* @title Checkbox
* @brief A checked and a disabled checkbox, plus a two-way bound pair.
*
* Each checkbox gets its caption from the `text` property. The first starts
* `checked="true"`, the second is `disabled="true"`. The last two share
* `bind_checked="subject_checked"`, so ticking one ticks the other.
*/
void lv_example_checkbox(void)
{
static lv_style_t style_tick_checked;
static lv_subject_t subject_checked;
static bool inited = false;
if (!inited) {
/*Init all styles*/
lv_style_init(&style_tick_checked);
lv_style_set_bg_color(&style_tick_checked, lv_color_hex(0x16a34a));
lv_style_set_border_color(&style_tick_checked, lv_color_hex(0x16a34a));
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, 14, 0);
lv_obj_set_style_pad_all(screen, 18, 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);
/* Checked by default */
lv_obj_t * lv_checkbox_0 = lv_checkbox_create(screen);
lv_checkbox_set_text(lv_checkbox_0, "Enable feature");
lv_obj_set_state(lv_checkbox_0, LV_STATE_CHECKED, true);
lv_obj_add_style(lv_checkbox_0, &style_tick_checked, LV_PART_INDICATOR | LV_STATE_CHECKED);
/* Disabled */
lv_obj_t * lv_checkbox_1 = lv_checkbox_create(screen);
lv_checkbox_set_text(lv_checkbox_1, "Unavailable option");
lv_obj_set_state(lv_checkbox_1, LV_STATE_DISABLED, true);
/* Two checkboxes bound to the same subject */
lv_obj_t * lv_checkbox_2 = lv_checkbox_create(screen);
lv_checkbox_set_text(lv_checkbox_2, "Sync A");
lv_obj_bind_checked(lv_checkbox_2, &subject_checked);
lv_obj_add_style(lv_checkbox_2, &style_tick_checked, LV_PART_INDICATOR | LV_STATE_CHECKED);
lv_obj_t * lv_checkbox_3 = lv_checkbox_create(screen);
lv_checkbox_set_text(lv_checkbox_3, "Sync B");
lv_obj_bind_checked(lv_checkbox_3, &subject_checked);
lv_obj_add_style(lv_checkbox_3, &style_tick_checked, LV_PART_INDICATOR | LV_STATE_CHECKED);
}
<!--
@title Checkbox
@brief A checked and a disabled checkbox, plus a two-way bound pair.
Each checkbox gets its caption from the `text` property. The first starts
`checked="true"`, the second is `disabled="true"`. The last two share
`bind_checked="subject_checked"`, so ticking one ticks the other.
-->
<screen>
<styles>
<!-- Recolour the tick box (the INDICATOR part) when checked -->
<style name="style_tick_checked"
bg_color="0x16a34a" border_color="0x16a34a" />
</styles>
<view flex_flow="column" style_pad_row="14" style_pad_all="18" style_flex_track_place="center" style_flex_main_place="center">
<!-- Checked by default -->
<lv_checkbox text="Enable feature" checked="true">
<style name="style_tick_checked" selector="checked|indicator" />
</lv_checkbox>
<!-- Disabled -->
<lv_checkbox text="Unavailable option" disabled="true" />
<!-- Two checkboxes bound to the same subject -->
<lv_checkbox text="Sync A" bind_checked="subject_checked">
<style name="style_tick_checked" selector="checked|indicator" />
</lv_checkbox>
<lv_checkbox text="Sync B" bind_checked="subject_checked">
<style name="style_tick_checked" selector="checked|indicator" />
</lv_checkbox>
</view>
</screen>
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 label and its area: text properties; pad_column sets the gap between the tick box and the text. |
indicator | The tick box: background, border, shadow and padding properties. bg_image_src sets the check icon. Use the 'checked' state (e.g. indicator|checked) for the checked look. |
Properties below are the widget's XML <api>; see API for how properties, parameters and elements work.
| Property | Type | Description |
|---|---|---|
text | string | The label text shown next to the checkbox |
Last updated on