Drop-down List (lv_dropdown)
Shows a selected value and opens a list to pick one option.
Overview
The Drop-down List shows a single selected value and, when activated, opens a list of options for the user to pick from. The list closes automatically once a new option is selected.
Learn more in the LVGL Open documentation.
/**
* @file lv_example_dropdown.c
*/
#include "../lv_examples.h"
/**
* @title Drop-down list
* @brief Options, a preselected item, a styled list, and subject binding.
*
* `options` is a newline-separated string; `selected` picks the initial
* index. `bind_value="subject_index"` two-ways the selection, and the label
* below reads the same subject. The `<lv_dropdown-list>` element styles the
* open list (the popup) separately from the closed button.
*/
void lv_example_dropdown(void)
{
static lv_style_t style_dd;
static lv_style_t style_dd_list;
static lv_style_t style_dd_list_selected;
static lv_style_t style_dd_list_pressed;
static lv_subject_t subject_index;
static bool inited = false;
if (!inited) {
/*Init all styles*/
lv_style_init(&style_dd);
lv_style_init(&style_dd_list);
lv_style_init(&style_dd_list_selected);
lv_style_init(&style_dd_list_pressed);
lv_style_set_bg_color(&style_dd, lv_color_hex(0x1f2937));
lv_style_set_bg_opa(&style_dd, (255 * 100 / 100));
lv_style_set_text_color(&style_dd, lv_color_hex(0xf3f4f6));
lv_style_set_radius(&style_dd, 8);
lv_style_set_pad_all(&style_dd, 10);
lv_style_set_bg_color(&style_dd_list, lv_color_hex(0x111827));
lv_style_set_text_color(&style_dd_list, lv_color_hex(0xe5e7eb));
lv_style_set_border_color(&style_dd_list, lv_color_hex(0x3b82f6));
lv_style_set_border_width(&style_dd_list, 1);
lv_style_set_bg_color(&style_dd_list_selected, lv_color_hex(0xa93bf5));
lv_style_set_text_color(&style_dd_list_selected, lv_color_hex(0xffffff));
lv_style_set_bg_color(&style_dd_list_pressed, lv_color_hex(0xcc98f1));
lv_style_set_text_color(&style_dd_list_pressed, lv_color_hex(0xffffff));
lv_subject_init_int(&subject_index, 0);
inited = true;
}
lv_obj_t * screen = lv_screen_active();
lv_obj_t * lv_dropdown_0 = lv_dropdown_create(screen);
lv_obj_set_width(lv_dropdown_0, 200);
lv_obj_set_align(lv_dropdown_0, LV_ALIGN_CENTER);
lv_dropdown_set_options(lv_dropdown_0, "Celsius\nFahrenheit\nKelvin");
lv_dropdown_set_selected(lv_dropdown_0, 1);
lv_dropdown_bind_value(lv_dropdown_0, &subject_index);
lv_obj_add_style(lv_dropdown_0, &style_dd, 0);
lv_obj_t * lv_dropdown_list_0 = lv_dropdown_get_list(lv_dropdown_0);
lv_obj_add_style(lv_dropdown_list_0, &style_dd_list, 0);
lv_obj_add_style(lv_dropdown_list_0, &style_dd_list_selected, LV_PART_SELECTED | LV_STATE_CHECKED);
lv_obj_add_style(lv_dropdown_list_0, &style_dd_list_pressed, LV_PART_SELECTED | LV_STATE_PRESSED);
lv_obj_t * lv_label_0 = lv_label_create(screen);
lv_label_bind_text(lv_label_0, &subject_index, "Selected index: %d");
lv_obj_set_align(lv_label_0, LV_ALIGN_CENTER);
lv_obj_set_y(lv_label_0, -40);
}
<!--
@title Drop-down list
@brief Options, a preselected item, a styled list, and subject binding.
`options` is a newline-separated string; `selected` picks the initial
index. `bind_value="subject_index"` two-ways the selection, and the label
below reads the same subject. The `<lv_dropdown-list>` element styles the
open list (the popup) separately from the closed button.
-->
<screen>
<styles>
<style name="style_dd" bg_color="0x1f2937" bg_opa="100%" text_color="0xf3f4f6" radius="8" pad_all="10" />
<!-- The opened list popup -->
<style
name="style_dd_list"
bg_color="0x111827"
text_color="0xe5e7eb"
border_color="0x3b82f6"
border_width="1"
/>
<style name="style_dd_list_selected" bg_color="0xa93bf5" text_color="0xffffff" />
<style name="style_dd_list_pressed" bg_color="0xcc98f1" text_color="0xffffff" />
</styles>
<view>
<lv_dropdown
width="200"
align="center"
options="Celsius Fahrenheit Kelvin"
selected="1"
bind_value="subject_index"
>
<style name="style_dd" />
<lv_dropdown-list>
<style name="style_dd_list" />
<style name="style_dd_list_selected" selector="selected|checked" />
<style name="style_dd_list_pressed" selector="selected|pressed" />
</lv_dropdown-list>
</lv_dropdown>
<lv_label bind_text="subject_index" bind_text-fmt="Selected index: %d" align="center" y="-40" />
</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 closed button: background and text properties. Gets the 'checked' state while the list is open. |
indicator | The symbol shown next to the text (set via symbol): image_recolor and image_recolor_opa. |
Properties below are the widget's XML <api>; see API for how properties, parameters and elements work.
| Property | Type | Description |
|---|---|---|
text | string | Set text displayed instead of the selected option label |
options | string | Set options as a newline (\n) separated string |
selected | int | Select an option by index (0-based) |
symbol | image | Set a symbol shown next to the dropdown |
bind_value | subject | Bind the selected option index to a subject |
dir | enum:lv_dir | Tells in which direction the dropdown should be opened |
selected_highlight | bool | Highlight the selected option in the open list |
text_translation_tag | string | Translation tag of the text shown on the button. It follows the active language. |
options_translation_tag | string | Translation tag of the options. Its translations are newline (\n) separated lists. It follows the active language. |
<lv_dropdown-list>
The dropdown list object for styling or customization
access get · returns lv_obj
Parts
| Part | Description |
|---|---|
main | The open list background: background, border and padding. max_height caps the open list size. |
scrollbar | The list's scrollbar: width (thickness), background properties and padding on the respective side. |
selected | The highlighted option: background and text properties. The 'checked' state styles the currently selected option and 'pressed' the one being pressed/keyed. |
No attributes — created as an empty child.
Last updated on