# Roller (lv_roller) (/widgets/roller)



Overview [#overview]

Roller allows the end user to select an item from a list by scrolling through it.
The item in the middle is the selected item and normally stands out from
the other items due to different styles applied to it.

Parts and Styles [#parts-and-styles]

* <ApiLink name="LV_PART_MAIN" /> The background of the roller uses the [typical
  background](/common-widget-features/styles/overview) and text style properties.

  * Style `text_line_space` adjusts the space between list items.
    Use <ApiLink name="lv_obj_set_style_text_line_space" /> to set this value.

  * When the Roller is scrolled and doesn't stop exactly on an item, it will
    automatically scroll to the nearest valid item in `anim_time`
    milliseconds as specified in the `anim_duration` style.  Use
    <ApiLink name="lv_obj_set_style_anim_duration" /> to set this value.
* <ApiLink name="LV_PART_SELECTED" /> The selected item (displayed in the  middle of
  the Roller).  Besides the typical background properties, it uses text style
  properties to change the appearance of the text of the selected item.

Usage [#usage]

Setting the list items [#setting-the-list-items]

List items are passed to the Roller as a string with
<ApiLink name="lv_roller_set_options" display="lv_roller_set_options(roller, string_list, LV_ROLLER_MODE_NORMAL)" />.
The items should be separated by `\n`. For example:
`"First\nSecond\nThird"`.

<ApiLink name="LV_ROLLER_MODE_INFINITE" /> makes the Roller circular.

You can select an option programmatically with
<ApiLink name="lv_roller_set_selected" display="lv_roller_set_selected(roller, id, LV_ANIM_ON)" />,
where *id* is the zero-based index of the list item to be selected.

If you don't know the index of an option can also select an item with
<ApiLink name="lv_roller_set_selected_str" display="lv_roller_set_selected_str(roller, str, LV_ANIM_ON)" />,
where *str* is the string equal to one of the list items.

Get selected option [#get-selected-option]

To get the *index* of the currently selected item use <ApiLink name="lv_roller_get_selected" display="lv_roller_get_selected(roller)" />.

<ApiLink name="lv_roller_get_selected_str" display="lv_roller_get_selected_str(roller, buf, buf_size)" /> will copy the name of the selected item to `buf`.

Visible rows [#visible-rows]

The number of visible rows can be adjusted with <ApiLink name="lv_roller_set_visible_row_count" display="lv_roller_set_visible_row_count(roller, num)" />.

This function calculates the height with the current style. If the font,
line space, border width, etc. of the Roller changes, this function needs
to be called again.

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 Roller Widget's integer
value directly.  Note that this is a two-way binding (Subject \<===> Widget) so an end
user's direct interaction with the Roller Widget updates the Subject's value and vice
versa.

It supports only integer subjects.

* <ApiLink name="lv_roller_bind_value" display="lv_roller_bind_value(roller, &subject)" />

Events [#events]

* <ApiLink name="LV_EVENT_VALUE_CHANGED" /> Sent when a new list item is selected.

<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 option
* `LV_KEY_LEFT/UP` Select previous option
* <ApiLink name="LY_KEY_ENTER" /> Accept the selected option (sends <ApiLink name="LV_EVENT_VALUE_CHANGED" /> event)

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

Examples [#examples]

Simple Roller [#simple-roller]

<LvglExample name="lv_example_roller_1" path="widgets/roller/lv_example_roller_1" />

Styling the roller [#styling-the-roller]

<LvglExample name="lv_example_roller_2" path="widgets/roller/lv_example_roller_2" />

add fade mask to roller [#add-fade-mask-to-roller]

<LvglExample name="lv_example_roller_3" path="widgets/roller/lv_example_roller_3" />

API [#api]
