Overview

The Switch can bu used to turn on/off something. The look like a little slider. The state of the switch can be changed by:

  • Clicking on it
  • Slide it
  • orlv_sw_on(sw) and lv_sw_off(sw) functions.

A callback function can be assigned to call when the user uses the switch: lv_sw_set_action(sw, my_action)

Style usage

You can modify the Switch's styles with lv_sw_set_style(sw, LV_SW_STYLE_..., &style)

  • LV_SW_STYLE_BG Style of the background. All style.body properties are used. The padding values make the Switch smaller then the knob. (negative value makes is larger)
  • LV_SW_STYLE_INDIC Style of the indicator. All style.body properties are used. The padding values make the indicator smaller then the background.
  • LV_SW_STYLE_KNOB_OFF Style of the knob when the switch is off. The style.body properties are used except padding.
  • LV_SW_STYLE_KNOB_ON Style of the knob when the switch is on. The style.body properties are used except padding.

Notes

  • The Knob is not a real object it is only drawn above the Bar
Example of Switch in Littlev Graphics Library

						/*Create styles for the switch*/
static lv_style_t bg_style;
static lv_style_t indic_style;
static lv_style_t knob_on_style;
static lv_style_t knob_off_style;
lv_style_copy(&bg_style, &lv_style_pretty);
bg_style.body.radius = LV_RADIUS_CIRCLE;

lv_style_copy(&indic_style, &lv_style_pretty_color);
indic_style.body.radius = LV_RADIUS_CIRCLE;
indic_style.body.main_color = LV_COLOR_HEX(0x9fc8ef);
indic_style.body.grad_color = LV_COLOR_HEX(0x9fc8ef);
indic_style.body.padding.hor = 0;
indic_style.body.padding.ver = 0;

lv_style_copy(&knob_off_style, &lv_style_pretty);
knob_off_style.body.radius = LV_RADIUS_CIRCLE;
knob_off_style.body.shadow.width = 4;
knob_off_style.body.shadow.type = LV_SHADOW_BOTTOM;

lv_style_copy(&knob_on_style, &lv_style_pretty_color);
knob_on_style.body.radius = LV_RADIUS_CIRCLE;
knob_on_style.body.shadow.width = 4;
knob_on_style.body.shadow.type = LV_SHADOW_BOTTOM;

/*Create a switch and apply the styles*/
lv_obj_t *sw1 = lv_sw_create(lv_scr_act(), NULL);
lv_sw_set_style(sw1, LV_SW_STYLE_BG, &bg_style);
lv_sw_set_style(sw1, LV_SW_STYLE_INDIC, &indic_style);
lv_sw_set_style(sw1, LV_SW_STYLE_KNOB_ON, &knob_on_style);
lv_sw_set_style(sw1, LV_SW_STYLE_KNOB_OFF, &knob_off_style);
lv_obj_align(sw1, NULL, LV_ALIGN_CENTER, 0, -50);

/*Copy the first switch and turn it ON*/
lv_obj_t *sw2 = lv_sw_create(lv_scr_act(), sw1);
lv_sw_set_on(sw2);
lv_obj_align(sw2, NULL, LV_ALIGN_CENTER, 0, 50);

lv_obj_t *

lv_sw_create

(lv_obj_t * par, lv_obj_t * copy);

Create a switch objects

par pointer to an object, it will be the parent of the new switch

copy pointer to a switch object, if not NULL then the new object will be copied from it

return pointer to the created switch

void

lv_sw_on

(lv_obj_t *sw);

Turn ON the switch

sw pointer to a switch object

void

lv_sw_off

(lv_obj_t *sw);

Turn OFF the switch

sw pointer to a switch object

void

lv_sw_set_action

(lv_obj_t * sw, lv_action_t action);

Set a function which will be called when the switch is toggled by the user

sw pointer to switch object

action a callback function

void

lv_sw_set_style

(lv_obj_t *sw, lv_sw_style_t type, lv_style_t *style);

Set a style of a switch

sw pointer to a switch object

type which style should be set

style pointer to a style

bool

lv_sw_get_state

(lv_obj_t *sw);

Get the state of a switch

sw pointer to a switch object

return false: OFF; true: ON

lv_action_t

lv_sw_get_action

(lv_obj_t * slider);

Get the switch action function

slider pointer to a switch object

return the callback function

lv_style_t *

lv_sw_get_style

(lv_obj_t *sw, lv_sw_style_t type);

Get a style of a switch

sw pointer to a switch object

type which style should be get

return style pointer to a style

LittlevGL - Open-source Embedded GUI Library

LittlevGL is a free and open-source graphics library providing everything you need to create embedded GUI with easy-to-use graphical elements, beautiful visual effects and low memory footprint.

If you like LittlevGL, please
support its deveopment!


The founder of Littlev Graphics Library (LittlevGL) and related software modules is:
Gábor Kiss-Vámosi
All Rights Reserved ©  2018 Hungary