Button Matrix (lv_buttonmatrix)
Many buttons in rows and columns drawn virtually from a single text map.
Overview
The Button Matrix displays many buttons arranged in rows and columns from a single text map. The buttons are drawn virtually rather than created as individual widgets, making it a very memory-efficient way to show a grid of buttons.
Learn more in the LVGL Open documentation.
/**
* @file lv_example_buttonmatrix.c
*/
#include "../lv_examples.h"
/**
* @title Button matrix
* @brief A keypad from a single map, with per-button width and a disabled key.
*
* `map` lists the button labels; `'\n'` starts a new row. `ctrl_map` gives
* one control entry per button (in order): here the last row's `0` takes
* `width_2` to span double, and `Clear` is `disabled`. Buttons are drawn
* virtually, so the whole pad is one lightweight widget.
*/
void lv_example_buttonmatrix(void)
{
static lv_style_t style_bm;
static lv_style_t style_keys;
static lv_style_t style_keys_pr;
static bool inited = false;
if (!inited) {
/*Init all styles*/
lv_style_init(&style_bm);
lv_style_init(&style_keys);
lv_style_init(&style_keys_pr);
lv_style_set_bg_color(&style_bm, lv_color_hex(0x0f172a));
lv_style_set_bg_opa(&style_bm, (255 * 100 / 100));
lv_style_set_radius(&style_bm, 8);
lv_style_set_pad_all(&style_bm, 8);
lv_style_set_bg_color(&style_keys, lv_color_hex(0x313d51));
lv_style_set_text_color(&style_keys, lv_color_hex(0xe2e8f0));
lv_style_set_radius(&style_keys, 6);
lv_style_set_shadow_width(&style_keys, 4);
lv_style_set_bg_color(&style_keys_pr, lv_color_hex(0x3b82f6));
inited = true;
}
lv_obj_t * screen = lv_screen_active();
lv_obj_set_style_flex_cross_place(screen, LV_FLEX_ALIGN_CENTER, 0);
lv_obj_set_style_pad_all(screen, 14, 0);
lv_obj_t * lv_buttonmatrix_0 = lv_buttonmatrix_create(screen);
lv_obj_set_size(lv_buttonmatrix_0, 260, 170);
lv_obj_set_align(lv_buttonmatrix_0, LV_ALIGN_CENTER);
static const char *lv_buttonmatrix_0_map_0[] = {"7", "8", "9", "\n", "4", "5", "6", "\n", "1", "2", "3", "\n", "0", "Clear", NULL};
lv_buttonmatrix_set_map(lv_buttonmatrix_0, lv_buttonmatrix_0_map_0);
static const lv_buttonmatrix_ctrl_t lv_buttonmatrix_0_ctrl_map_0[] = {LV_BUTTONMATRIX_CTRL_NONE, LV_BUTTONMATRIX_CTRL_NONE, LV_BUTTONMATRIX_CTRL_NONE, LV_BUTTONMATRIX_CTRL_NONE, LV_BUTTONMATRIX_CTRL_NONE, LV_BUTTONMATRIX_CTRL_NONE, LV_BUTTONMATRIX_CTRL_NONE, LV_BUTTONMATRIX_CTRL_NONE, LV_BUTTONMATRIX_CTRL_NONE, LV_BUTTONMATRIX_CTRL_WIDTH_2, LV_BUTTONMATRIX_CTRL_DISABLED};
lv_buttonmatrix_set_ctrl_map(lv_buttonmatrix_0, lv_buttonmatrix_0_ctrl_map_0);
lv_obj_add_style(lv_buttonmatrix_0, &style_bm, 0);
lv_obj_add_style(lv_buttonmatrix_0, &style_keys, LV_PART_ITEMS);
lv_obj_add_style(lv_buttonmatrix_0, &style_keys_pr, LV_PART_ITEMS | LV_STATE_PRESSED);
}
<!--
@title Button matrix
@brief A keypad from a single map, with per-button width and a disabled key.
`map` lists the button labels; `'\n'` starts a new row. `ctrl_map` gives
one control entry per button (in order): here the last row's `0` takes
`width_2` to span double, and `Clear` is `disabled`. Buttons are drawn
virtually, so the whole pad is one lightweight widget.
-->
<screen>
<styles>
<style name="style_bm" bg_color="0x0f172a" bg_opa="100%" radius="8" pad_all="8" />
<!-- The individual buttons (the "items" part) -->
<style name="style_keys" bg_color="0x313d51" text_color="0xe2e8f0" radius="6" shadow_width="4" />
<style name="style_keys_pr" bg_color="0x3b82f6" />
</styles>
<view style_flex_cross_place="center" style_pad_all="14">
<lv_buttonmatrix
width="260"
height="170"
align="center"
map="'7' '8' '9' ' ' '4' '5' '6' ' ' '1' '2' '3' ' ' '0' 'Clear'"
ctrl_map="none none none none none none none none none width_2 disabled"
>
<style name="style_bm" />
<style name="style_keys" selector="items" />
<style name="style_keys_pr" selector="items|pressed" />
</lv_buttonmatrix>
</view>
</screen>
Enumerations
lv_buttonmatrix_ctrl — Control flags you can apply to individual buttons (values can be combined)
| Value | Description |
|---|---|
none | Make the button use the default state with no special control |
width_1 | Make the button take 1 unit of relative width in its row |
width_2 | Make the button take 2 units of relative width in its row |
width_3 | Make the button take 3 units of relative width in its row |
width_4 | Make the button take 4 units of relative width in its row |
width_5 | Make the button take 5 units of relative width in its row |
width_6 | Make the button take 6 units of relative width in its row |
width_7 | Make the button take 7 units of relative width in its row |
width_8 | Make the button take 8 units of relative width in its row |
width_9 | Make the button take 9 units of relative width in its row |
width_10 | Make the button take 10 units of relative width in its row |
width_11 | Make the button take 11 units of relative width in its row |
width_12 | Make the button take 12 units of relative width in its row |
width_13 | Make the button take 13 units of relative width in its row |
width_14 | Make the button take 14 units of relative width in its row |
width_15 | Make the button take 15 units of relative width in its row |
hidden | Make the button hidden |
no_repeat | Disable long-press repeat on the button |
disabled | Make the button disabled (not clickable) |
checkable | Make the button toggleable when clicked |
checked | Keep the button in the toggled (checked) state |
click_trig | Trigger LV_EVENT_VALUE_CHANGED on CLICK instead of PRESS |
popover | Show a popover when pressing the button |
recolor | Enable text recoloring with inline #color codes |
reserved_1 | Reserved flag for future use |
reserved_2 | Reserved flag for future use |
custom_1 | Custom flag free to use by the user |
custom_2 | Custom flag free to use by the user |
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 behind the buttons: background properties. pad_row and pad_column set the gaps between buttons. |
items | The buttons: text and background properties (transforms are not applied). Combine with the 'checked', 'pressed', 'focused' and 'disabled' states, e.g. items|checked. |
Properties below are the widget's XML <api>; see API for how properties, parameters and elements work.
| Property | Type | Description |
|---|---|---|
map | string[NULL] | Set the layout map of the button matrix using text strings and \n for line breaks (e.g. 'a' 'b' 'c') |
ctrl_map | enum:lv_buttonmatrix_ctrl[]+ | Set the control flags for each button in the map |
one_checked | bool | Force exactly one button to stay checked at all times |
selected_button | int | Select a button by index (new lines do not count) |
Last updated on