Checkbox (lv_checkbox)
Overview
The Checkbox Widget is created from a "tick box" and a label. When the Checkbox is clicked the tick box is toggled.
Parts and Styles
LV_PART_MAIN
Background of Checkbox and it uses the text- and all the typical background-style properties.pad_column
adjusts spacing between tickbox and labelLV_PART_INDICATOR
The "tick box" is a square that uses all the typical background style properties. By default, its size is equal to the height of the main part's font. Padding properties make the tick box larger in the respective directions.
The Checkbox is added to the default group (if one is set).
Usage
Text
The text can be modified with lv_checkbox_set_text(cb, "New text") and will be dynamically allocated.
To set static text, use lv_checkbox_set_text_static(cb, txt). This
way, only a pointer to txt
will be stored. The provided text buffer must remain
available for the life of the Checkbox.
Check, uncheck, disable
You can programmatically check, un-check, and disable the Checkbox by using the common state add/clear function:
lv_obj_add_state(cb, LV_STATE_CHECKED); /* Make Checkbox checked */
lv_obj_remove_state(cb, LV_STATE_CHECKED); /* Make Checkbox unchecked */
lv_obj_add_state(cb, LV_STATE_CHECKED | LV_STATE_DISABLED); /* Make Checkbox checked and disabled */
To find out whether the Checkbox is checked use lv_obj_has_state(cb, LV_STATE_CHECKED).
Events
LV_EVENT_VALUE_CHANGED
Sent when Checkbox is toggled.
Further Reading
Learn more about Base-Widget Events emitted by all Widgets.
Learn more about Events.
Keys
The following Keys are processed by Checkbox:
LV_KEY_RIGHT/UP
Go to CHECKED state if Checkbox is enabledLV_KEY_LEFT/DOWN
Go to non-CHECKED state if Checkbox is enabledLV_KEY_ENTER
Clicks the Checkbox and toggles its value.
Note that, as usual, the state of LV_KEY_ENTER
is translated to
LV_EVENT_PRESSED/PRESSING/RELEASED
etc.
Further Reading
Learn more about Keys.
Example
Simple Checkboxes
C code
View on GitHub#include "../../lv_examples.h"
#if LV_USE_CHECKBOX && LV_BUILD_EXAMPLES
static void event_handler(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * obj = lv_event_get_target(e);
if(code == LV_EVENT_VALUE_CHANGED) {
LV_UNUSED(obj);
const char * txt = lv_checkbox_get_text(obj);
const char * state = lv_obj_get_state(obj) & LV_STATE_CHECKED ? "Checked" : "Unchecked";
LV_UNUSED(txt);
LV_UNUSED(state);
LV_LOG_USER("%s: %s", txt, state);
}
}
void lv_example_checkbox_1(void)
{
lv_obj_set_flex_flow(lv_screen_active(), LV_FLEX_FLOW_COLUMN);
lv_obj_set_flex_align(lv_screen_active(), LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER);
lv_obj_t * cb;
cb = lv_checkbox_create(lv_screen_active());
lv_checkbox_set_text(cb, "Apple");
lv_obj_add_event_cb(cb, event_handler, LV_EVENT_ALL, NULL);
cb = lv_checkbox_create(lv_screen_active());
lv_checkbox_set_text(cb, "Banana");
lv_obj_add_state(cb, LV_STATE_CHECKED);
lv_obj_add_event_cb(cb, event_handler, LV_EVENT_ALL, NULL);
cb = lv_checkbox_create(lv_screen_active());
lv_checkbox_set_text(cb, "Lemon");
lv_obj_add_state(cb, LV_STATE_DISABLED);
lv_obj_add_event_cb(cb, event_handler, LV_EVENT_ALL, NULL);
cb = lv_checkbox_create(lv_screen_active());
lv_obj_add_state(cb, LV_STATE_CHECKED | LV_STATE_DISABLED);
lv_checkbox_set_text(cb, "Melon\nand a new line");
lv_obj_add_event_cb(cb, event_handler, LV_EVENT_ALL, NULL);
lv_obj_update_layout(cb);
}
#endif