# The generated code sequence is incorrect, resulting in UI effects that do not match expectations

**URL:** <http://forum.squareline.io/t/the-generated-code-sequence-is-incorrect-resulting-in-ui-effects-that-do-not-match-expectations/5885>\
**Category:** Bugs\
**Created:** [May 10, 2025, 6:27am UTC](http://forum.squareline.io/t/the-generated-code-sequence-is-incorrect-resulting-in-ui-effects-that-do-not-match-expectations/5885 "2025-05-10T06:27:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kirl](http://forum.squareline.io/letter_avatar_proxy/v4/letter/k/3ab097/32.png) [@kirl](http://forum.squareline.io/u/kirl)\
**Post date:** [May 10, 2025, 6:27am UTC](http://forum.squareline.io/t/the-generated-code-sequence-is-incorrect-resulting-in-ui-effects-that-do-not-match-expectations/5885/1 "2025-05-10T06:27:42Z")

</div>

I created a switch and set the background color of the indicator in the checked state to theme color 1, and set the switch state to checked.  
However, the color displayed on the device is still the default color. After checking the generated UI code, I found that the code sequence is to first set the button status to checked and then set the color corresponding to the checked status, which led to the problem. The display effect achieved the expected result after manual adjustment.  
Generated problematic code:  
ui\_enableMotorSwitch = lv\_switch\_create(ui\_Screen1);  
lv\_obj\_set\_width(ui\_enableMotorSwitch, 50);  
lv\_obj\_set\_height(ui\_enableMotorSwitch, 25);  
lv\_obj\_set\_x(ui\_enableMotorSwitch, 92);  
lv\_obj\_set\_y(ui\_enableMotorSwitch, -96);  
lv\_obj\_set\_align(ui\_enableMotorSwitch, LV\_ALIGN\_CENTER);  
lv\_obj\_add\_state(ui\_enableMotorSwitch, LV\_STATE\_CHECKED); /// States

```
ui_object_set_themeable_style_property(ui_enableMotorSwitch, LV_PART_INDICATOR | LV_STATE_CHECKED, LV_STYLE_BG_COLOR,
                                       _ui_theme_color_color1);
ui_object_set_themeable_style_property(ui_enableMotorSwitch, LV_PART_INDICATOR | LV_STATE_CHECKED, LV_STYLE_BG_OPA,
                                       _ui_theme_alpha_color1);

```

Manually modified correct code:  
ui\_enableMotorSwitch = lv\_switch\_create(ui\_Screen1);  
lv\_obj\_set\_width(ui\_enableMotorSwitch, 50);  
lv\_obj\_set\_height(ui\_enableMotorSwitch, 25);  
lv\_obj\_set\_x(ui\_enableMotorSwitch, 92);  
lv\_obj\_set\_y(ui\_enableMotorSwitch, -96);  
lv\_obj\_set\_align(ui\_enableMotorSwitch, LV\_ALIGN\_CENTER);

```
ui_object_set_themeable_style_property(ui_enableMotorSwitch, LV_PART_INDICATOR | LV_STATE_CHECKED, LV_STYLE_BG_COLOR,
                                       _ui_theme_color_color1);
ui_object_set_themeable_style_property(ui_enableMotorSwitch, LV_PART_INDICATOR | LV_STATE_CHECKED, LV_STYLE_BG_OPA,
                                       _ui_theme_alpha_color1);
lv_obj_add_state(ui_enableMotorSwitch, LV_STATE_CHECKED); /// States

```

SquareLineStudio v1.5.1  
LVGL v9.1.0  
based on Arduino TFT\_eSPI

---

<div class="post-metadata">

**Author:** ![SquareLine\_Support](http://forum.squareline.io/user_avatar/forum.squareline.io/squareline_support/32/47_2.png) [@SquareLine\_Support](http://forum.squareline.io/u/SquareLine_Support)\
**Post date:** [May 13, 2025, 3:12pm UTC](http://forum.squareline.io/t/the-generated-code-sequence-is-incorrect-resulting-in-ui-effects-that-do-not-match-expectations/5885/2 "2025-05-13T15:12:30Z")

</div>

Hi, thanks for reporting this issue, kirl. Your fix is correct - setting the style properties before changing the state is the right approach. We’ll look into whether this is an LVGL issue or something we can fix in our code generation. For now, your workaround is the way to go.
