1# Drop-down list (lv_dropdown) 2 3 4## Overview 5 6The drop-down list allows the user to select one value from a list. 7 8The drop-down list is closed by default and displays a single value or a predefined text. 9When activated (by click on the drop-down list), a list is created from which the user may select one option. 10When the user selects a new value, the list is deleted again. 11 12The Drop-down list is added to the default group (if it is set). Besides the Drop-down list is an editable object to allow selecting an option with encoder navigation too. 13 14## Parts and Styles 15The Dropdown widget is built from the elements: "button" and "list" (both not related to the button and list widgets) 16 17### Button 18- `LV_PART_MAIN` The background of the button. Uses the typical background properties and text properties for the text on it. 19- `LV_PART_INDICATOR` Typically an arrow symbol that can be an image or a text (`LV_SYMBOL`). 20 21The button goes to `LV_STATE_CHECKED` when it's opened. 22 23### List 24- `LV_PART_MAIN` The list itself. Uses the typical background properties. `max_height` can be used to limit the height of the list. 25- `LV_PART_SCROLLBAR` The scrollbar background, border, shadow properties and width (for its own width) and right padding for the spacing on the right. 26- `LV_PART_SELECTED` Refers to the currently pressed, checked or pressed+checked option. Also uses the typical background properties. 27 28The list is hidden/shown on open/close. To add styles to it use `lv_dropdown_get_list(dropdown)` to get the list object. For example: 29 30```c 31lv_obj_t * list = lv_dropdown_get_list(dropdown) /*Get the list*/ 32lv_obj_add_style(list, &my_style, ...) /*Add the styles to the list*/}` 33``` 34 35Alternatively the theme can be extended with the new styles. 36 37## Usage 38 39## Overview 40 41### Set options 42Options are passed to the drop-down list as a string with `lv_dropdown_set_options(dropdown, options)`. Options should be separated by `\n`. For example: `"First\nSecond\nThird"`. This string will be saved in the drop-down list, so it can in a local variable. 43 44The `lv_dropdown_add_option(dropdown, "New option", pos)` function inserts a new option to `pos` index. 45 46To save memory the options can set from a static(constant) string too with `lv_dropdown_set_options_static(dropdown, options)`. 47In this case the options string should be alive while the drop-down list exists and `lv_dropdown_add_option` can't be used 48 49You can select an option manually with `lv_dropdown_set_selected(dropdown, id)`, where `id` is the index of an option. 50 51### Get selected option 52The get the *index* of the selected option, use `lv_dropdown_get_selected(dropdown)`. 53 54`lv_dropdown_get_selected_str(dropdown, buf, buf_size)` copies the *name* of the selected option to `buf`. 55 56### Direction 57The list can be created on any side. The default `LV_DIR_BOTTOM` can be modified by `lv_dropdown_set_dir(dropdown, LV_DIR_LEFT/RIGHT/UP/BOTTOM)` function. 58 59If the list would be vertically out of the screen, it will be aligned to the edge. 60 61### Symbol 62A symbol (typically an arrow) can be added to the dropdown list with `lv_dropdown_set_symbol(dropdown, LV_SYMBOL_...)` 63 64If the direction of the drop-down list is `LV_DIR_LEFT` the symbol will be shown on the left, otherwise on the right. 65 66### Show selected 67The main part can either show the selected option or a static text. If a static is set with `lv_dropdown_set_text(dropdown, "Some text")` it will be shown regardless to th selected option. 68If the text is `NULL` the selected option is displayed on the button. 69 70### Manually open/close 71To manually open or close the drop-down list the `lv_dropdown_open/close(dropdown)` function can be used. 72 73## Events 74Apart from the [Generic events](../overview/event.html#generic-events), the following [Special events](../overview/event.html#special-events) are sent by the drop-down list: 75- `LV_EVENT_VALUE_CHANGED` Sent when the new option is selected or the list is opened/closed. 76- `LV_EVENT_CANCEL` Sent when the list is closed 77- `LV_EVENT_READY` Sent when the list is opened 78 79See the events of the [Base object](/widgets/obj) too. 80 81Learn more about [Events](/overview/event). 82 83## Keys 84- `LV_KEY_RIGHT/DOWN` Select the next option. 85- `LV_KEY_LEFT/UP` Select the previous option. 86- `LY_KEY_ENTER` Apply the selected option (Sends `LV_EVENT_VALUE_CHANGED` event and closes the drop-down list). 87 88Learn more about [Keys](/overview/indev). 89 90## Example 91 92```eval_rst 93 94.. include:: ../../../examples/widgets/dropdown/index.rst 95 96``` 97 98## API 99 100```eval_rst 101 102.. doxygenfile:: lv_dropdown.h 103 :project: lvgl 104 105``` 106