# Drop-Down List (lv_dropdown) (/widgets/dropdown)



Overview [#overview]

The Drop-Down List allows the user to select a value from a list.

The Drop-Down List is closed by default and displays a single value or
predefined text. When activated (by click on the Drop-Down List), a list
is created from which the user may select one item. When the user
selects a new value, the list is deleted again.

The Drop-down list is added to the default group (if one is set).
The Drop-down list is an editable Widget allowing list-item selection via
encoder or keyboard navigation as well.

Parts and Styles [#parts-and-styles]

The Drop-Down List Widget is built from the elements: "button" and "list"
(lightweight versions of the Button and List Widgets).

Button [#button]

* <ApiLink name="LV_PART_MAIN" /> Background of button, uses the [typical
  background](/common-widget-features/styles/overview) and text style properties for its text.
* <ApiLink name="LV_PART_INDICATOR" /> Typically an arrow symbol that can be an Image
  or text (e.g. <ApiLink name="LV_SYMBOL" />).

The button goes to <ApiLink name="LV_STATE_CHECKED" /> when it's opened.

List [#list]

* <ApiLink name="LV_PART_MAIN" /> The list itself; uses the [typical background
  style properties](/common-widget-features/styles/overview). `max_height` can be used to limit the
  height of the list.
* <ApiLink name="LV_PART_SCROLLBAR" /> The scrollbar background, border, shadow
  properties and width (for its own width) and right padding for the
  spacing on the right.
* <ApiLink name="LV_PART_SELECTED" /> Refers to the currently pressed, checked or
  pressed+checked option.  Also uses the [typical background style properties](/common-widget-features/styles/overview).

The list is shown/hidden on open/close. To add styles to it use
<ApiLink name="lv_dropdown_get_list" display="lv_dropdown_get_list(dropdown)" /> to get the list object.  Example:

```c title=" " lineNumbers=1
lv_obj_t * list = lv_dropdown_get_list(dropdown) /* Get list */
lv_obj_add_style(list, &my_style, selector)      /* Add styles to list */
```

Alternatively the theme can be extended with new styles.

Usage [#usage]

List items [#list-items]

The list items are passed to the Drop-Down List as a newline-separated list in a string
as the `options` argument to <ApiLink name="lv_dropdown_set_options" display="lv_dropdown_set_options(dropdown, options)" />.
Each list item should be separated by `\n`.  Example: `"First\nSecond\nThird"`.
This string is copied by the Drop-Down List, so its contents do not need to remain
available beyond this call.

The <ApiLink name="lv_dropdown_add_option" display="lv_dropdown_add_option(dropdown, &#x22;New option&#x22;, pos)" /> function
inserts a new option at index `pos`.

To save memory the options can be set from a static (const) string as well
with <ApiLink name="lv_dropdown_set_options_static" display="lv_dropdown_set_options_static(dropdown, options)" />. In this case
the options string's contents must remain available for the life of the Drop-Down
List and <ApiLink name="lv_dropdown_add_option" /> cannot be used.

You can select an option programmatically with
<ApiLink name="lv_dropdown_set_selected" display="lv_dropdown_set_selected(dropdown, id)" />, where `id` is the index of
the target option.

Get selected option [#get-selected-option]

To get the *index* of the selected option, use
<ApiLink name="lv_dropdown_get_selected" display="lv_dropdown_get_selected(dropdown)" />.

<ApiLink name="lv_dropdown_get_selected_str" display="lv_dropdown_get_selected_str(dropdown, buf, buf_size)" /> copies the
*name* of the selected option to `buf`.

Direction [#direction]

The list can be created on any side. The default <ApiLink name="LV_DIR_BOTTOM" /> can
be modified using <ApiLink name="lv_dropdown_set_dir" display="lv_dropdown_set_dir(dropdown, LV_DIR_LEFT)" />.

If the list would be vertically out of the screen, it will be aligned to
the edge.

Symbol [#symbol]

A symbol (typically an arrow) can be added to the Drop-Down List with

<ApiLink name="lv_dropdown_set_symbol" display="lv_dropdown_set_symbol(dropdown, LV_SYMBOL_...)" />

If the direction of the Drop-Down List is <ApiLink name="LV_DIR_LEFT" /> the symbol
will be shown on the left, otherwise on the right.

Show selected [#show-selected]

The main part can either show the selected item or fixed text. If
fixed text is set with <ApiLink name="lv_dropdown_set_text" display="lv_dropdown_set_text(dropdown, &#x22;Some text&#x22;)" />
or <ApiLink name="lv_dropdown_set_text_static" display="lv_dropdown_set_text_static(dropdown, &#x22;Some text&#x22;)" /> it
will be shown regardless of the selected item. If the text is `NULL`
the selected option is displayed on the button.

Programmatically open/close [#programmatically-openclose]

To programmatically open or close the Drop-Down List use
<ApiLink name="lv_dropdown_open" display="lv_dropdown_open(dropdown)" /> or <ApiLink name="lv_dropdown_close" display="lv_dropdown_close(dropdown)" />.

Data binding [#data-binding]

To get familiar with observers, subjects, and data bindings in general visit the
[Observer](/main-modules/observer/observer) page.

This method of subscribing to an integer Subject affects a Drop-Down Widget's integer
value directly.  Note that this is a two-way binding (Subject \<===> Widget) so an end
user's direct interaction with the Drop-Down Widget updates the Subject's value and
vice versa.

It support only integer Subjects.

* <ApiLink name="lv_dropdown_bind_value" display="lv_dropdown_bind_value(dropdown, &subject)" />

Events [#events]

* <ApiLink name="LV_EVENT_VALUE_CHANGED" /> Sent when a new option is selected or the list is opened/closed.
* <ApiLink name="LV_EVENT_CANCEL" /> Sent when list is closed.
* <ApiLink name="LV_EVENT_READY" /> Sent when list is opened.

<Callout type="info" title="Further Reading">
  Learn more about [Events](/common-widget-features/events) emitted by all Widgets.

  Learn more about [events](/common-widget-features/events).
</Callout>

Keys [#keys]

* `LV_KEY_RIGHT/DOWN` Select next list item.
* `LV_KEY_LEFT/UP` Select previous list item.
* <ApiLink name="LV_KEY_ENTER" /> Apply selected list item (sends
  <ApiLink name="LV_EVENT_VALUE_CHANGED" /> event and closes Drop-Down List).

<Callout type="info" title="Further Reading">
  Learn more about [Keys](/main-modules/indev/keypad).
</Callout>

Examples [#examples]

Simple Drop down list [#simple-drop-down-list]

<LvglExample name="lv_example_dropdown_1" path="widgets/dropdown/lv_example_dropdown_1" />

Drop down in four directions [#drop-down-in-four-directions]

<LvglExample name="lv_example_dropdown_2" path="widgets/dropdown/lv_example_dropdown_2" />

Menu [#menu]

<LvglExample name="lv_example_dropdown_3" path="widgets/dropdown/lv_example_dropdown_3" />

API [#api]
