How to change button/object color on demand

Hello everyone. I have a few buttons on a simple LVGL page I created. I want the buttons to change colours (and stay changed) when I click on them. I then want the colour to change again when I click on it a second time. So basically I want the button to load up with a default colour, then when pressed it should switch to another colour (signifying it is in a different mode), then when pressed again, to go back to the original colour.

I found out how to change colour based on pressed, focused, etc states, but none seem to do what I need it to do. I was thinking of manually changing the colour of the button in the button object callback function (this function is called whenever the button is pressed), but I don’t know how to manually change the colour whenever I want.

Can someone tell me if there is a way to do this?

I think local styles (i.e. lv_obj_set_style_local_bg_color) are what you’re looking for.

1 Like

That works, but after I use that, I cannot change it another time. As you can see in my code, I use a signal variable to keep track of what color mode the button is in.

However, when I click the button for a second time, the color does not change. I know the ‘else if’ statement (where the button should change a second time) is triggering properly because my printf line is executing.

Do you know why the color does not change when I click the the button a second time?


There was a bug in the library that prevented setting local styles multiple times in a special case. I’ve fixed it in the master branch.