2021-04-12 18:19:04 +02:00
```eval_rst
.. include:: /header.rst
:github_url: |github_link_base|/widgets/dropdown.md
```
# Drop-down list (lv_dropdown)
## Overview
The drop-down list allows the user to select one value from a list.
The drop-down list is closed by default and displays a single value or a predefined text.
When activated (by click on the drop-down list), a list is created from which the user may select one option.
2021-06-09 15:10:35 +02:00
When the user selects a new value, the list is deleted again.
2021-04-12 18:19:04 +02:00
2021-06-09 15:10:35 +02:00
The 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.
2021-05-03 17:32:57 +02:00
2021-04-12 18:19:04 +02:00
## Parts and Styles
2021-06-09 15:10:35 +02:00
The Dropdown widget is built from the elements: "button" and "list" (both not related to the button and list widgets)
2021-04-12 18:19:04 +02:00
2021-05-03 17:32:57 +02:00
### Button
2021-06-09 15:10:35 +02:00
- `LV_PART_MAIN` The background of the button. Uses the typical background properties and text properties for the text on it.
2021-05-03 17:32:57 +02:00
- `LV_PART_INDICATOR` Typically an arrow symbol that can be an image or a text (`LV_SYMBOL` ).
2021-04-12 18:19:04 +02:00
2021-08-26 10:52:39 +02:00
The button goes to `LV_STATE_CHECKED` when it's opened.
2021-04-12 18:19:04 +02:00
2021-05-03 17:32:57 +02:00
### List
2021-06-09 15:10:35 +02:00
- `LV_PART_MAIN` The list itself. Uses the typical background properties. `max_height` can be used to limit the height of the list.
- `LV_PART_SCROLLBAR` The scrollbar background, border, shadow properties and width (for its own width) and right padding for the spacing on the right.
- `LV_PART_SELECTED` Refers to the currently pressed, checked or pressed+checked option. Also uses the typical background properties.
2021-04-12 18:19:04 +02:00
2021-11-23 13:30:16 +01:00
The 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:
2021-07-28 10:09:46 +02:00
```c
lv_obj_t * list = lv_dropdown_get_list(dropdown) /*Get the list*/
lv_obj_add_style(list, & my_style, ...) /*Add the styles to the list*/}`
```
2021-04-12 18:19:04 +02:00
2021-06-09 15:10:35 +02:00
Alternatively the theme can be extended with the new styles.
2021-05-03 17:32:57 +02:00
2021-04-12 18:19:04 +02:00
## Usage
## Overview
### Set options
2021-06-09 15:10:35 +02:00
Options 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.
2021-04-12 18:19:04 +02:00
The `lv_dropdown_add_option(dropdown, "New option", pos)` function inserts a new option to `pos` index.
To save memory the options can set from a static(constant) string too with `lv_dropdown_set_static_options(dropdown, options)` .
2021-06-09 15:10:35 +02:00
In this case the options string should be alive while the drop-down list exists and `lv_dropdown_add_option` can't be used
2021-04-12 18:19:04 +02:00
2021-05-03 17:32:57 +02:00
You can select an option manually with `lv_dropdown_set_selected(dropdown, id)` , where `id` is the index of an option.
2021-04-12 18:19:04 +02:00
### Get selected option
2021-06-09 15:10:35 +02:00
The get the *index* of the selected option, use `lv_dropdown_get_selected(dropdown)` .
2021-04-12 18:19:04 +02:00
2021-06-09 15:10:35 +02:00
`lv_dropdown_get_selected_str(dropdown, buf, buf_size)` copies the *name* of the selected option to `buf` .
2021-04-12 18:19:04 +02:00
### Direction
2021-05-03 17:32:57 +02:00
The 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.
2021-04-12 18:19:04 +02:00
2021-06-09 15:10:35 +02:00
If the list would be vertically out of the screen, it will be aligned to the edge.
2021-04-12 18:19:04 +02:00
### Symbol
2021-08-26 10:52:39 +02:00
A symbol (typically an arrow) can be added to the dropdown list with `lv_dropdown_set_symbol(dropdown, LV_SYMBOL_...)`
2021-04-12 18:19:04 +02:00
2021-06-09 15:10:35 +02:00
If the direction of the drop-down list is `LV_DIR_LEFT` the symbol will be shown on the left, otherwise on the right.
2021-04-12 18:19:04 +02:00
### Show selected
2021-05-03 17:32:57 +02:00
The 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.
2021-06-09 15:10:35 +02:00
If the text is `NULL` the selected option is displayed on the button.
2021-04-12 18:19:04 +02:00
### Manually open/close
2021-05-03 17:32:57 +02:00
To manually open or close the drop-down list the `lv_dropdown_open/close(dropdown)` function can be used.
2021-04-12 18:19:04 +02:00
## Events
2021-06-09 15:10:35 +02:00
Apart 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:
2021-05-03 17:32:57 +02:00
- `LV_EVENT_VALUE_CHANGED` Sent when the new option is selected or the list is opened/closed.
2021-07-28 10:09:46 +02:00
- `LV_EVENT_CANCEL` Sent when the list is closed
2021-11-11 14:09:40 +01:00
- `LV_EVENT_READY` Sent when the list is opened
2021-04-12 18:19:04 +02:00
2021-07-07 16:18:56 +02:00
See the events of the [Base object ](/widgets/obj ) too.
2021-04-12 18:19:04 +02:00
Learn more about [Events ](/overview/event ).
## Keys
2021-05-03 17:32:57 +02:00
- `LV_KEY_RIGHT/DOWN` Select the next option.
- `LV_KEY_LEFT/UP` Select the previous option.
2021-06-09 15:10:35 +02:00
- `LY_KEY_ENTER` Apply the selected option (Sends `LV_EVENT_VALUE_CHANGED` event and closes the drop-down list).
2021-05-03 17:32:57 +02:00
Learn more about [Keys ](/overview/indev ).
2021-04-12 18:19:04 +02:00
## Example
```eval_rst
2021-05-03 17:32:57 +02:00
.. include:: ../../../examples/widgets/dropdown/index.rst
2021-04-12 18:19:04 +02:00
```
## API
```eval_rst
.. doxygenfile:: lv_dropdown.h
:project: lvgl
```