Events

Learn how to handle and trigger events in LVGL Pro Editor using XML.

Respond to user interactions by defining events on widgets that trigger callbacks, navigate between screens, or update application state when specific actions occur.

Overview

There are several ways to define events for user interactions. These events can be added as children of any widget.

Triggers

Every event element takes a trigger attribute, which says when the action should happen. It accepts three things: an event name, a condition, or a one time switch.

An event name

This is the usual case: the name of an LVGL event, mapped straight from its enum.

  • "clicked" - LV_EVENT_CLICKED, and the default when trigger is missing
  • "pressed" - LV_EVENT_PRESSED
  • "value_changed" - LV_EVENT_VALUE_CHANGED
  • "all" - LV_EVENT_ALL
  • and so on for every LVGL event

A condition

An expression that reads a subject is watched instead of an event. The action runs on every change from false to true:

xml
<!-- Show the alarm screen as soon as the temperature goes over 30 -->
<screen_load_event trigger="{subject_temp > 30}" screen="screen_alarm"/>

Condition triggers require LVGL Pro Editor v2.1 and LVGL v9.6.

Two things to keep in mind:

  • Only the rising edge fires. Going back to false does nothing, and neither does a change that leaves the condition true.
  • A condition that is already true when the element is created fires right away. The remembered previous state starts as false, so subscribing counts as the first rising edge.

A one time switch

An expression that reads no subject can't change, so it is decided once, when the element is created. What it decides depends on what it evaluates to.

A boolean switches the whole element on or off:

  • true - the action runs immediately, once
  • false - nothing is attached at all, so the element does nothing
xml
<!-- Load the home screen at creation, but only when this instance asked for it -->
<screen_load_event trigger="{auto_open}" screen="screen_home"/>

<!-- `{true}` never changes, so this is simply "run once, on creation" -->
<event_cb callback="on_ready" trigger="{true}"/>

Anything else is used as an event name, so an expression can pick which event to listen to:

xml
<!-- Fire on a long press in compact mode, on a plain click otherwise -->
<event_cb callback="my_handler" trigger="{compact ? long_pressed : clicked}"/>

Call Function

User-defined functions can be called in response to events like this:

xml
<view>
 <lv_button width="200" height="100">
  <event_cb callback="my_callback_1" trigger="clicked" user_data="some_text"/>
  <lv_label text="Hello"/>
 </lv_button>
</view>

The callback must follow the standard LVGL event callback signature:

 
void an_event_handler(lv_event_t * e);

In exported C code, a function with the exact name is assumed to defined by the user. For example, callback="my_callback_1" will be exported as:

 
void my_callback_1(lv_event_t * e); /* At the beginning of the exported file */

lv_obj_add_event_cb(obj, my_callback_1, LV_EVENT_CLICKED, "some_text");

And it's the developer's responsibility to implement the my_callback_1.

When the trigger is a condition there is no LVGL event behind the call, so LVGL builds one. The callback still gets the right target and its own user_data, but the event code it carries is created internally and is not one of the LV_EVENT_* names, so don't read it.

Recommendations

It's a good idea to add event function in the <project_name>.c file, created by the editor to keep all the related functions in one place. If there are a lot of event functions it's recommended to create multiple files for them.

To distinguish simulator builds from builds for an embedded device add defines like #define BUILD_SIMULATOR and wrap the event callbacks to #if BUILD_SIMULATOR.

The user_data attribute is optional. If omitted, NULL will be passed.

Screen Load and Create Events

Use the screen_load_event and screen_create_event tags as children of a widget or component to load or create screens in response to user interactions (such as a click).

The key difference between load and create is:

  • load - Loads an already existing screen. After leaving the screen, it remains in memory, so all states are preserved. The screen to load must have <screen permanent="true"/>
  • create - Creates the screen dynamically, and when leaving the screen, it is deleted, so all changes are lost (unless saved in subjects). The screen to load must have <screen permanent="false"/> (false is the default).

Both tags support these optional attributes:

  • trigger - When to act. An event name, or a condition. See Triggers. Default: "clicked".
  • anim_type - How the screen is loaded (e.g., "move_right", "fade_in"). Default: "none".
  • duration - Length of the animation in milliseconds. Default: 0. Only used if anim_type is not "none".
  • delay - Wait time before loading the screen in milliseconds. Default: 0.

Here is an example of both load and create events:

xml
<!-- screen1.xml -->
<screen permanent="true">
 <view style_bg_color="0xff7788">
  <lv_button>
   <lv_label text="Create"/>
   <!-- Create an instance of "screen2" and load it. -->
   <screen_create_event screen="screen2" anim_type="over_right" duration="500" delay="1000"/>
  </lv_button>
 </view>
</screen>

<!-- screen2.xml -->
<screen>
 <view style_bg_color="0x77ff88">
  <lv_button>
   <lv_label text="Load"/>
   <!-- Load an already created instance of screen1. -->
   <screen_load_event screen="screen1"/>
  </lv_button>
 </view>
</screen>

Set a Subject Value

<set_subject_event> writes a subject when the trigger fires:

xml
<view>
 <lv_button width="200" height="100">
  <set_subject_event subject="subject_count" value="{subject_count + 1}" trigger="clicked"/>
  <lv_label text="One more"/>
 </lv_button>
</view>

It takes three attributes:

  • subject - The subject to write. Mandatory.
  • value - The value to write: a literal, or a { } expression re-evaluated every time the trigger fires. Mandatory.
  • trigger - See Triggers. Default: "clicked".

Because value is a fresh expression on every fire, one element covers setting, toggling, and incrementing:

xml
<!-- Set: a literal, like any other attribute -->
<set_subject_event subject="subject_name" value="Anna"/>

<!-- Toggle -->
<set_subject_event subject="subject_menu_open" value="{!subject_menu_open}"/>

<!-- Increment, and decrement with a negative step -->
<set_subject_event subject="subject_count" value="{subject_count + 1}"/>
<set_subject_event subject="subject_count" value="{subject_count - 1}"/>

<!-- Anything else an expression can say -->
<set_subject_event subject="subject_page" value="{subject_page + 1}" trigger="{subject_next_pressed}"/>

The subject's declared type decides how the value is written, and true and false become 1 and 0 in an int subject. To keep a value in range, give the subject a min_value and a max_value: every write is clamped to them.

Older subject events

Before <set_subject_event> there was one element per operation. They still work, so existing XML keeps running:

xml
<subject_set_int_event trigger="clicked" subject="subject_int" value="10"/>
<subject_set_float_event trigger="clicked" subject="subject_float" value="12.34"/>
<subject_set_string_event trigger="clicked" subject="subject_string" value="Hello"/>
<subject_toggle_event trigger="clicked" subject="subject_int"/>
<subject_increment_event trigger="clicked" subject="subject_int" step="10" min_value="0" max_value="50" rollover="true"/>

subject_increment_event is the only one with something <set_subject_event> can't do: rollover="true" wraps the value to the other end at the min_value/max_value boundary instead of stopping there.

These elements take an event name only. A { } condition in their trigger doesn't work, so use <set_subject_event> when you need one.

Play Animation

Playing Timelines can be triggered by events (such as clicks) using the <play_timeline_event> element as a child of any widget.

Specify a target UI element and select one of its timelines to play. If target="self", the timeline is looked up in the current Component, or Screen (in the current XML file).

This example defines a timeline, and plays it on itself.

xml
<component>
	<animations>
		<timeline name="timeline_1">
			<animation target="self" duration="1000" prop="translate_x" start="100" end="0" />
			<animation target="main_button" duration="1000" prop="opa" start="0" end="255" />
		</timeline>
	</animations>

	<view>
		<lv_button name="main_button">
			<play_timeline_event trigger="clicked" timeline="timeline_1" target="self" />
			<lv_label text="Play" />
		</lv_button>
	</view>
</component>

Optionally a delay property can be set as well (in milliseconds), and the timeline can be played in reversed too:

xml
<play_timeline_event trigger="clicked" timeline="timeline_1" target="self" reverse="true" delay="500"/>

Last updated on

On this page