Button color change

Description

Hi i create button and I want the button color to change when I click on the button but esp32 reboots when I click the button on the screen

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

2.8inch_ESP32-2432S028R

What LVGL version are you using?

8.3.9

What do you want to achieve?

What have you tried so far?

Code to reproduce

Add a code snippet which can run in the simulator. It should contain only the relevant code that compiles without errors when separated from your main code base.

The code block(s) should be formatted like:

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

  if (code == LV_EVENT_CLICKED) {
    client.subscribe(mqtt_topic);
    client.setCallback(callback);
    if (buttonState) {
      client.publish(mqtt_topic, "0");
      static lv_style_t style_btn_red2;
      lv_style_init(&style_btn_red2);
      lv_style_set_bg_color(&style_btn_red2, lv_color_make(0, 255, 0)); // BLUE RED GREEN 
      lv_style_set_bg_opa(&style_btn_red2, LV_OPA_COVER);
      lv_obj_add_style(btn, &style_btn_red2, 0);
    } else {
      client.publish(mqtt_topic, "1");
    }
  }
}

Screenshot and/or video

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

You’re creating a new button in the callback. Instead, you should try using

	lv_obj_t *btn = lv_event_get_target(event);

to get the button related to the event and try to change the color of this button.

C:\Users\*r\Desktop\buton_3_0\buton_3_0.ino:435:18: error: redeclaration of 'lv_obj_t* btn'
       lv_obj_t * btn = lv_btn_create(lv_scr_act());
                  ^~~
C:\Users\*r\Desktop\buton_3_0\buton_3_0.ino:434:17: note: 'lv_obj_t* btn' previously declared here
       lv_obj_t *btn = lv_event_get_target(event);
                 ^~~

exit status 1

how to use this code

Okay, so in the code you posted the btn was never actually created, right? So you should get the pointer to the button that the event triggered, and this is done with the line I posted before.

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

  if (code == LV_EVENT_CLICKED) {
    client.subscribe(mqtt_topic);
    client.setCallback(callback);
    if (buttonState) {
lv_obj_t *btn = lv_event_get_target(e); //<-- add this line here
      client.publish(mqtt_topic, "0");
      static lv_style_t style_btn_red2;
      lv_style_init(&style_btn_red2);
      lv_style_set_bg_color(&style_btn_red2, lv_color_make(0, 255, 0)); // BLUE RED GREEN 
      lv_style_set_bg_opa(&style_btn_red2, LV_OPA_COVER);
      lv_obj_add_style(btn, &style_btn_red2, 0);
    } else {
      client.publish(mqtt_topic, "1");
    }
  }
}

Unless the button btn in line lv_obj_add_style(btn, &style_btn_red2, 0); is actually static

nope i created button in another function
and i need change button color when i clicked same button

  1. Did my change help?
  2. You created the btn outside of the scope of this callback, so it wasn’t available, or was it static?

nothing changing and btn static

i need complate example for button color change but i cant found