How to have a checkable button that doesn't change colors?

Description

I am having a checkable button that I want to have a fixed color, however it changes color on any other long press when the button is in PRESSED + CHECKED state.

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

Reproduced on both STM32 and the simulator on windows. Doesn’t seem to be platform dependent.

What LVGL version are you using?

#define LVGL_VERSION_MAJOR 7
#define LVGL_VERSION_MINOR 8
#define LVGL_VERSION_PATCH 1
#define LVGL_VERSION_INFO “”

What do you want to achieve?

The button stay at one specific color that I set.

What have you tried so far?

Tried to set the same color for all states of the button.

Code to reproduce


        lv_obj_t* lv_button = lv_btn_create(lv_scr_act(), NULL);
	lv_obj_set_size(lv_button, 100, 100);
	lv_obj_align(lv_button, NULL, LV_ALIGN_CENTER, 0, 0);
	lv_btn_set_checkable(lv_button, true);

	static lv_style_t style;

	lv_style_init(&style);
	lv_style_set_bg_color(&style, LV_STATE_DEFAULT, LV_COLOR_BLACK);
	lv_style_set_bg_color(&style, LV_STATE_PRESSED, LV_COLOR_BLACK);
	lv_style_set_bg_color(&style, LV_STATE_CHECKED, LV_COLOR_BLACK);
	lv_style_set_bg_color(&style, LV_STATE_DISABLED, LV_COLOR_BLACK);
	lv_style_set_bg_color(&style, LV_STATE_EDITED, LV_COLOR_BLACK);
	lv_style_set_bg_color(&style, LV_STATE_FOCUSED, LV_COLOR_BLACK);
	lv_style_set_bg_color(&style, LV_STATE_HOVERED, LV_COLOR_BLACK);

	lv_style_set_border_width(&style, LV_STATE_DEFAULT, 0);
	lv_style_set_outline_width(&style, LV_STATE_DEFAULT, 0);

	lv_obj_add_style(lv_button, LV_OBJ_PART_MAIN, &style);

Screenshot and/or video

This is how it looks in stable state. To reproduce, press the button for a few seconds and release. Do it a few times and notice how it becomes turquoise on every other press.

image

image

Anybody has any suggestion how to fix this?

I think you may also need to handle the combination of pressed and checked.

lv_style_set_bg_color(&style, LV_STATE_CHECKED | LV_STATE_PRESSED, LV_COLOR_BLACK);

Thanks @embeddedt, I will give it a try.

Yes, that worked. Thanks @embeddedt.