Button (lv_button)
A clickable Base Widget with defaults tuned for its role.
Overview
A Button is a clickable Base Widget with defaults tuned for its role: it is not scrollable, is added to the default input group, and sizes itself to its content. Place a Label or image inside it for the caption.
Learn more in the LVGL Open documentation.
/**
* @file lv_example_button.c
*/
#include "../lv_examples.h"
/**
* @title Button
* @brief A normal, a toggleable and a disabled button with per-state styling.
*
* Button itself adds no properties over the Base Widget — its behaviour comes
* from flags and styling. The middle button is `checkable="true"` so a tap
* toggles it, starts `checked="true"`, and a `selector="checked"` style
* recolours it. The third uses `disabled="true"` to show the muted,
* non-interactive state.
*/
void lv_example_button(void)
{
static lv_style_t style_btn;
static lv_style_t style_btn_pressed;
static lv_style_t style_btn_checked;
static bool inited = false;
if (!inited) {
/*Init all styles*/
lv_style_init(&style_btn);
lv_style_init(&style_btn_pressed);
lv_style_init(&style_btn_checked);
lv_style_set_bg_color(&style_btn, lv_color_hex(0x3b82f6));
lv_style_set_bg_opa(&style_btn, (255 * 100 / 100));
lv_style_set_radius(&style_btn, 8);
lv_style_set_pad_hor(&style_btn, 18);
lv_style_set_pad_ver(&style_btn, 10);
lv_style_set_text_color(&style_btn, lv_color_hex(0xffffff));
lv_style_set_bg_color(&style_btn_pressed, lv_color_hex(0x1d4ed8));
lv_style_set_bg_color(&style_btn_checked, lv_color_hex(0x16a34a));
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_flex_cross_place(screen, LV_FLEX_ALIGN_CENTER, 0);
lv_obj_set_style_flex_main_place(screen, LV_FLEX_ALIGN_CENTER, 0);
lv_obj_set_style_flex_track_place(screen, LV_FLEX_ALIGN_CENTER, 0);
/* Plain action button */
lv_obj_t * lv_button_0 = lv_button_create(screen);
lv_obj_add_style(lv_button_0, &style_btn, 0);
lv_obj_add_style(lv_button_0, &style_btn_pressed, LV_STATE_PRESSED);
lv_obj_t * lv_label_0 = lv_label_create(lv_button_0);
lv_label_set_text(lv_label_0, "Action");
/* Toggle button: starts checked, keeps its state */
lv_obj_t * lv_button_1 = lv_button_create(screen);
lv_obj_set_flag(lv_button_1, LV_OBJ_FLAG_CHECKABLE, true);
lv_obj_set_state(lv_button_1, LV_STATE_CHECKED, true);
lv_obj_add_style(lv_button_1, &style_btn, 0);
lv_obj_add_style(lv_button_1, &style_btn_pressed, LV_STATE_PRESSED);
lv_obj_add_style(lv_button_1, &style_btn_checked, LV_STATE_CHECKED);
lv_obj_t * lv_label_1 = lv_label_create(lv_button_1);
lv_label_set_text(lv_label_1, "Toggle");
/* Disabled button: not clickable, dimmed */
lv_obj_t * lv_button_2 = lv_button_create(screen);
lv_obj_set_state(lv_button_2, LV_STATE_DISABLED, true);
lv_obj_set_style_bg_opa(lv_button_2, (255 * 40 / 100), 0);
lv_obj_add_style(lv_button_2, &style_btn, 0);
lv_obj_t * lv_label_2 = lv_label_create(lv_button_2);
lv_label_set_text(lv_label_2, "Disabled");
}
<!--
@title Button
@brief A normal, a toggleable and a disabled button with per-state styling.
Button itself adds no properties over the Base Widget — its behaviour comes
from flags and styling. The middle button is `checkable="true"` so a tap
toggles it, starts `checked="true"`, and a `selector="checked"` style
recolours it. The third uses `disabled="true"` to show the muted,
non-interactive state.
-->
<screen>
<styles>
<!-- Shared button look -->
<style
name="style_btn"
bg_color="0x3b82f6"
bg_opa="100%"
radius="8"
pad_hor="18"
pad_ver="10"
text_color="0xffffff"
/>
<!-- Pressed: darken -->
<style name="style_btn_pressed" bg_color="0x1d4ed8" />
<!-- Checked: accent -->
<style name="style_btn_checked" bg_color="0x16a34a" />
</styles>
<view
flex_flow="column"
style_pad_row="14"
style_flex_cross_place="center"
style_flex_main_place="center"
style_flex_track_place="center"
>
<!-- 💡 Tap the middle button: it keeps its checked colour until tapped again. -->
<!-- Plain action button -->
<lv_button>
<style name="style_btn" />
<style name="style_btn_pressed" selector="pressed" />
<lv_label text="Action" />
</lv_button>
<!-- Toggle button: starts checked, keeps its state -->
<lv_button checkable="true" checked="true">
<style name="style_btn" />
<style name="style_btn_pressed" selector="pressed" />
<style name="style_btn_checked" selector="checked" />
<lv_label text="Toggle" />
</lv_button>
<!-- Disabled button: not clickable, dimmed -->
<lv_button disabled="true" style_bg_opa="40%">
<style name="style_btn" />
<lv_label text="Disabled" />
</lv_button>
</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 | Style the whole button: background, border, outline, shadow and padding. Combine with state selectors (pressed, checked, disabled, focused) for feedback. |
This widget adds no XML properties of its own — it inherits the full API of the Base Widget (lv_obj). See API for how the <api> works.
Last updated on