Change the text on button


Change the label(or text) on button

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


What LVGL version are you using?


What do you want to achieve?

Trying to change the label(or text) on button after a click

What have you tried so far?

lv_label_set_text(lv_label_create(btn), “Clicked !”);

Code to reproduce

When button is clicked:
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * btn = lv_event_get_target(e);
if(code == LV_EVENT_CLICKED) {
lv_label_set_text(lv_label_create(btn), “Clicked !”);
But, it did not delete the original text. It wrote over it !!!
I also, tried to recreate style, and add it to the button, but got same behavior.
I tried to write NULL text, to maybe clear the original text, then tried to write the new text as follows:
lv_label_set_text(lv_label_create(btn), NULL);
lv_label_set_text(lv_label_create(btn), “Clicked !”);
But, this trick did not work.


static lv_obj_t *btn, *label;

static void btn_handler(lv_event_t *e)
  lv_event_code_t code = lv_event_get_code(e);

  if (code == LV_EVENT_CLICKED)
    lv_label_set_text(label, "Clicked!");

btn = lv_btn_create(parent);
lv_obj_add_event_cb(btn, btn_handler, LV_EVENT_ALL, NULL);

label = lv_label_create(btn);
lv_label_set_text(label, "Click on button");

You can also do this using the lv_event_get_user_data function.

You can’t do that, do it like this: lv_label_set_text(label, "text").

Thanks for the code.
The lv_label_set_text(…) works. It does write the text, but the problem it writes it on top of the existing label.
Maybe I need to call another method to clear the existing text before writing new text ??
Or maybe the version I am using (8.0.2) has bug
I am not sure, because I am new to lvgl but if that is working on your system, then it must be the second possibility.

Ensure you aren’t creating a new label object each time. You only need to create it once; after that, just update the text with lv_label_set_text.

You got it.
Yes, I was actually creating new label object in each case.
I just replaced it with just one as static, and it is now working properly.
Thank you very much.

You can also create a non-static label.
lv_obj_t *label


label = lv_label_create(parent)
lv_label_set_text(label, "Text")

and add an event to the button and when clicked, change the label text: lv_label_set_text(label, "Clicked!")

Note: you cannot create multiple labels with the same variable name as there will be confusion when changing the label text.