Spinbox (lv_spinbox)
A numeric editor built on a Text Area with fixed decimals and step.
Overview
The Spinbox is a numeric editor built on a Text Area. It shows an integer with an optional fixed decimal point and digit count, and the value can be stepped within a range.
Learn more in the LVGL Open documentation.
/**
* @file lv_example_spinbox.c
*/
#include "../lv_examples.h"
/**
* @title Spinbox
* @brief A fixed-point spinbox and a bound one linked to a slider.
*
* The first spinbox sets `digit_count` and `dec_point_pos` for a fixed
* decimal point, with a `min_value`/`max_value` range, a `step`, and
* `rollover="true"` so it wraps at the limits. The second uses
* `bind_value="subject_value"`; a slider bound to the same subject moves it.
*/
void lv_example_spinbox(void)
{
LV_FONT_DECLARE(font_large);
static lv_style_t style_sb;
static lv_subject_t subject_value;
static bool inited = false;
if (!inited) {
/*Init all styles*/
lv_style_init(&style_sb);
lv_style_set_bg_color(&style_sb, lv_color_hex(0x111827));
lv_style_set_bg_opa(&style_sb, (255 * 100 / 100));
lv_style_set_text_color(&style_sb, lv_color_hex(0xf9fafb));
lv_style_set_radius(&style_sb, 8);
lv_style_set_pad_all(&style_sb, 10);
lv_style_set_text_font(&style_sb, &font_large);
lv_subject_init_int(&subject_value, 50);
lv_subject_set_min_value_int(&subject_value, 0);
lv_subject_set_max_value_int(&subject_value, 100);
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, 16, 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);
/* Fixed-point: 4 digits, point 2 places from the right */
lv_obj_t * lv_spinbox_0 = lv_spinbox_create(screen);
lv_obj_set_width(lv_spinbox_0, 150);
lv_spinbox_set_digit_count(lv_spinbox_0, 4);
lv_spinbox_set_dec_point_pos(lv_spinbox_0, 2);
lv_spinbox_set_min_value(lv_spinbox_0, 0);
lv_spinbox_set_max_value(lv_spinbox_0, 9999);
lv_spinbox_set_step(lv_spinbox_0, 25);
lv_spinbox_set_rollover(lv_spinbox_0, true);
lv_spinbox_set_value(lv_spinbox_0, 1250);
lv_obj_add_style(lv_spinbox_0, &style_sb, 0);
/* Bound to subject_value, driven by the slider below */
lv_obj_t * lv_spinbox_1 = lv_spinbox_create(screen);
lv_obj_set_width(lv_spinbox_1, 150);
lv_spinbox_set_min_value(lv_spinbox_1, 0);
lv_spinbox_set_max_value(lv_spinbox_1, 100);
lv_spinbox_bind_value(lv_spinbox_1, &subject_value);
lv_obj_add_style(lv_spinbox_1, &style_sb, 0);
lv_obj_t * lv_slider_0 = lv_slider_create(screen);
lv_obj_set_width(lv_slider_0, 220);
lv_slider_bind_value(lv_slider_0, &subject_value);
}
<!--
@title Spinbox
@brief A fixed-point spinbox and a bound one linked to a slider.
The first spinbox sets `digit_count` and `dec_point_pos` for a fixed
decimal point, with a `min_value`/`max_value` range, a `step`, and
`rollover="true"` so it wraps at the limits. The second uses
`bind_value="subject_value"`; a slider bound to the same subject moves it.
-->
<screen>
<styles>
<style
name="style_sb"
bg_color="0x111827"
bg_opa="100%"
text_color="0xf9fafb"
radius="8"
pad_all="10"
text_font="font_large"
/>
</styles>
<view
flex_flow="column"
style_pad_row="16"
style_pad_all="16"
style_flex_cross_place="center"
style_flex_track_place="center"
style_flex_main_place="center"
>
<!-- Fixed-point: 4 digits, point 2 places from the right -->
<lv_spinbox
width="150"
digit_count="4"
dec_point_pos="2"
min_value="0"
max_value="9999"
step="25"
rollover="true"
value="1250"
>
<style name="style_sb" />
</lv_spinbox>
<!-- Bound to subject_value, driven by the slider below -->
<lv_spinbox width="150" min_value="0" max_value="100" bind_value="subject_value">
<style name="style_sb" />
</lv_spinbox>
<lv_slider width="220" bind_value="subject_value" />
</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 background and the number text (extends lv_textarea): background, padding and text properties. |
scrollbar | The scrollbar shown when content overflows: width (thickness), background properties and padding. |
selected | The selected text. Only text_color and bg_color are used. |
cursor | The cursor over the edited digit: bg_color/bg_opa for a block cursor or a left border for a bar cursor. |
Properties below are the widget's XML <api>; see API for how properties, parameters and elements work.
| Property | Type | Description |
|---|---|---|
value | int | Set the value as a raw integer. The displayed format depends on digit_count and dec_point_pos |
rollover | bool | If enabled, stepping past the maximum jumps to the minimum and vice versa |
digit_count | int | Set the number of digits, excluding the sign and the decimal separator |
dec_point_pos | int | Set the number of digits before the decimal point. 0 means no decimal point is shown |
min_value | int | Set the minimum value (inclusive) |
max_value | int | Set the maximum value (inclusive) |
step | int | Set the digit that changes on increment/decrement. Can be 1, 10, 100, etc. |
cursor_pos | int | Put the cursor on this digit (0 = rightmost). It also sets step |
digit_step_direction | enum:lv_dir | Which way the cursor moves when stepping to the next digit (left or right) |
bind_value | subject | Connect a subject to the spinbox's value |
Last updated on