Constants

Learn how to define and use constants in LVGL Pro Editor for colors, padding, and other reusable property values.

Constants allow you to define reusable values that can be referenced throughout your components and styles, making your code more maintainable and consistent.

Overview

Constants can be defined to replace any value with a selected type or to be used as special values. The supported types are:

  • int - 32bit integers
  • color - Color values (e.g., 0xff0000)
  • px - Pixel measurements
  • percentage - Percentage values
  • string - Text strings
  • opa - Opacity values
  • bool - Boolean true/false values

Defining Constants

Constants are defined within the <consts> tag:

xml
<consts>
  <color name="color1" value="0xff0000" help="Primary color"/>
  <px name="pad_xs" value="8" help="Small padding"/>
</consts>

Each constant has:

  • name - The identifier to reference this constant
  • value - The actual value
  • type - The value type (determined by the tag name)
  • help - Optional description for documentation

Using Constants

Constants can be used in:

  • Style properties
  • Widget properties
  • Component properties

A constant is referenced by its name in { }, where it can also be part of a larger expression. This works in the view and in the <consts>, <styles>, <subjects>, <animations> and <gradients> sections alike:

xml
<component>
  <consts>
    <color name="red" value="0xff0000" help="A pure red color"/>
    <int name="long" value="200" help="Just large number"/>
    <string name="greeting" value="Hi There!" help="Just be nice"/>

    <!-- A constant computed from another one -->
    <int name="half" value="{long / 2}"/>
  </consts>

  <styles>
    <style name="style_1" bg_color="{red}" pad_all="{half}"/>
  </styles>

  <view width="{long}">
    <style name="style_1"/>
    <lv_label text="{greeting}"/>
  </view>
</component>

#const_name is the legacy syntax for referencing a constant. It takes a single name only, so { } is the recommended form.

Last updated on

On this page