Switch (lv_switch)

Switch Widgets look like little sliders and are used to display, and optionally modify, a value that can be "on" or "off".

Edit on GitHub

Overview

Switch Widgets look like little sliders and are used to display, and optionally modify, a value that can be "on" or "off".

By default, a Switch is oriented horizontally. Its orientation will be vertical if you set width < height.

Parts and Styles

Usage

Change state

The Switch uses the standard LV_STATE_CHECKED state.

To get the current state of the Switch (with true being ON), use lv_obj_has_state(widget, LV_STATE_CHECKED).

Call lv_obj_add_state(widget, LV_STATE_CHECKED) to turn it on, or lv_obj_remove_state(widget, LV_STATE_CHECKED) to turn it off programmatically.

Change orientation

With a Switch is created, its default orientation is LV_SWITCH_ORIENTATION_AUTO, which causes it to be oriented based on width and height. You can change this behavior using lv_switch_set_orientation(widget, orientation). Possible values for orientation are:

Events

Further Reading

Learn more about Events emitted by all Widgets.

Learn more about events.

Keys

  • LV_KEY_UP/RIGHT Turns Switch ON
  • LV_KEY_DOWN/LEFT Turns Switch OFF
  • LV_KEY_ENTER Toggles the Switch

Further Reading

Learn more about Keys.

Examples

Simple Switch

Switch Orientation

API

How is this guide?

Last updated on

On this page