How to change color of a button, and restore later


Restore the color of a button to the default theme color

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


What LVGL version are you using?


What do you want to achieve?

I want to change the color of a button, and later restore the original color assigned in the theme

What have you tried so far?

I have changed the color succesfuly, but then I don’t know how to restore the original color. I haver reset the style, and then the button is not visible

Code to reproduce

		if (shortcut->NameBGColor.full != 0)
			lv_obj_set_style_local_bg_color( btnShort, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, pShortcut->NameBGColor);
			lv_obj_set_style_local_bg_color( btnShort, LV_OBJ_PART_MAIN, LV_STATE_CHECKED, pShortcut->NameBGColor);
			lv_obj_clean_style_list(btnShort, LV_OBJ_PART_MAIN);

Is there a way to restore the original color of the theme?



Use lv_obj_remove_style_local_prop.

Thank you! It worked as desired!


Though it worked, the function is not straightforward (if you don’t know exactly how it works). To deactivate the correct property I’ve had to do

		lv_obj_remove_style_local_prop(btnShort[i], LV_OBJ_PART_MAIN, LV_STYLE_BG_COLOR | (LV_STATE_DEFAULT << LV_STYLE_STATE_POS));
		lv_obj_remove_style_local_prop(btnShort[i], LV_OBJ_PART_MAIN, LV_STYLE_BG_COLOR | (LV_STATE_CHECKED << LV_STYLE_STATE_POS));

I didn’t know I had to change the OR and the shift, and which properties. Now I know that the style is the same I changed originaly LV_STYLE_### and then I have to add the OR and the state shifted LV_STYLE_STATE_POS.


There is a brief mention of it on the styles docs page but I agree that it could be more obvious. I’ll see what can be done to improve it.

1 Like

Yes, I found the mention, but I had a hard time figuring out, that’s why I posted it here (though I don’t know if I explianed much better).

Thank you.