Can we use one label in two screen?

Description

I want to creat a label and use it in two screen. this label in dynamic and I want t update it’s value in my application and show the new value in both screen. would you please tell me how can I do that?

What MCU/Processor/Board and compiler are you using?

ESP32- ARDUINO IDE

What LVGL version are you using?

8.3

What have you tried so far?

I creat a label and use it in two screens in two different positions. but in the display, the new value of that label is only updated on one screen.

Code to reproduce

//first screen
void ui_Screen1_screen_init(void)
{
    ui_Digital = lv_obj_create(NULL);
    lv_obj_clear_flag(ui_Digital, LV_OBJ_FLAG_SCROLLABLE);      /// Flags

    ui_day2 = lv_label_create(ui_Digital);
    lv_obj_set_width(ui_day2, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_day2, LV_SIZE_CONTENT);    /// 1
    lv_obj_set_x(ui_day2, 82);
    lv_obj_set_y(ui_day2, -56);
    lv_obj_set_align(ui_day2, LV_ALIGN_CENTER);
    lv_label_set_text(ui_day2, "");
    lv_obj_set_style_text_color(ui_day2, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui_day2, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_day2, &ui_font_DateBold, LV_PART_MAIN | LV_STATE_DEFAULT);
}

//second screen
void ui_Clock_screen_init(void)
{
    ui_Clock = lv_obj_create(NULL);
    lv_obj_clear_flag(ui_Clock, LV_OBJ_FLAG_SCROLLABLE);      /// Flags
ui_day2 = lv_label_create(ui_Digital);
    lv_obj_set_width(ui_day2, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_day2, LV_SIZE_CONTENT);    /// 1
    lv_obj_set_x(ui_day2, 50);
    lv_obj_set_y(ui_day2, -40);
    lv_obj_set_align(ui_day2, LV_ALIGN_CENTER);
    lv_label_set_text(ui_day2, "");
    lv_obj_set_style_text_color(ui_day2, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui_day2, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_day2, &ui_font_DateBold, LV_PART_MAIN | LV_STATE_DEFAULT);
}

//update the value of the label
  lv_label_set_text(ui_day2, timeWeekDay2);



## Screenshot and/or video
If possible, add screenshots and/or videos about the current state.

yes you can. you have to call lv_obj_set_parent(new parent) you would need to make the child or “shared” object a global instead of a local.

so something along these lines.

//first screen
void ui_Screen1_screen_init(void)
{
    ui_Digital = lv_obj_create(NULL);
    lv_obj_clear_flag(ui_Digital, LV_OBJ_FLAG_SCROLLABLE);      /// Flags

}

//second screen
void ui_Clock_screen_init(void)
{
    ui_Clock = lv_obj_create(NULL);
    lv_obj_clear_flag(ui_Clock, LV_OBJ_FLAG_SCROLLABLE);      /// Flags
}

void change_screen_to_clock(void) {
    lv_obj_set_parent(ui_day2, ui_Clock);
    lv_obj_set_align(ui_day2, LV_ALIGN_TOP_LEFT);
    lv_obj_set_x(ui_day2, 82);
    lv_obj_set_y(ui_day2, -56);
    lv_obj_set_align(ui_day2, LV_ALIGN_CENTER);
}

void change_screen_to_screen1(void) {
    lv_obj_set_parent(ui_day2, ui_Clock);
    lv_obj_set_align(ui_day2, LV_ALIGN_TOP_LEFT);
    lv_obj_set_x(ui_day2, 50);
    lv_obj_set_y(ui_day2, -40);
    lv_obj_set_align(ui_day2, LV_ALIGN_CENTER);
}

void create_shared_children(lv_obj_t * parent) {
    ui_day2 = lv_label_create(parent);
    lv_obj_set_width(ui_day2, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_day2, LV_SIZE_CONTENT);    /// 1
    lv_label_set_text(ui_day2, "");
    lv_obj_set_style_text_color(ui_day2, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui_day2, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_day2, &ui_font_DateBold, LV_PART_MAIN | LV_STATE_DEFAULT);
}

//update the value of the label
  lv_label_set_text(ui_day2, timeWeekDay2);