How to DISABLE a label

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

ARM STM32

What LVGL version are you using?

7.7

What do you want to achieve?

I want to be able to disable a label, and see it in another color. the label is beside a widget that is disabled, so I want both to appear as disabled.

What have you tried so far?

I have disable the label object, but the label remains the same. I have searched in the theme properties any way to add the disabled color of the labels, but I have not fund it

Code to reproduce

I have used

lv_obj_set_state(label, LV_STATE_DISABLED);

I have also changed the properties of the theme, to see if it works, but I’m not able to modify it properly

    lv_style_set_text_color(&styles->scr, LV_STATE_DEFAULT, COLOR_SCR_TEXT);
    lv_style_set_text_color(&styles->scr, LV_STATE_DISABLED, COLOR_SCR_TEXT_DIS);
    lv_style_set_value_color(&styles->scr, LV_STATE_DEFAULT, COLOR_SCR_TEXT);
    lv_style_set_value_color(&styles->scr, LV_STATE_DISABLED, COLOR_SCR_TEXT_DIS);

I am almost sure that there is a property to modify that can make the label appear in another color when disabled, but I have not found the way.

Anyone?

Alex

Nobody knows how to modify theme or label in a way that when you set the object to DISABLED, it the color is different?

Alex

Hi,

The following code seems to work from my testing. Does this help?

    lv_obj_t * label = lv_label_create(lv_scr_act(), NULL);
    lv_label_set_text(label, "hello world");
    lv_obj_set_state(label, LV_STATE_DISABLED);
    lv_obj_set_style_local_text_color(label, LV_LABEL_PART_MAIN, LV_STATE_DISABLED, lv_color_make(0xff, 0x00, 0x00));

Yes, this works.

But I would like to use a solution where the color is definied in the theme, for all the labels.

In the theme init, there is a section that calls

static void basic_init(void)
{
style_init_reset(&styles->scr);
lv_style_set_bg_opa(&styles->scr, LV_STATE_DEFAULT, LV_OPA_COVER);
lv_style_set_bg_color(&styles->scr, LV_STATE_DEFAULT, COLOR_SCR);
lv_style_set_text_color(&styles->scr, LV_STATE_DEFAULT, COLOR_SCR_TEXT);

I have added the line:

lv_style_set_text_color(&styles->scr, LV_STATE_DISABLED, COLOR_SCR_TEXT_DIS);

If I change COLOR_SCR_TEXT, I can see that the labels change their color in the screens. But I was expecting that id I addded the LV_STATE_DISABLED property, the COLOR_SCR_TEXT_DIS (current set to RED) color would apply when the label is disabled.Am I wrong? Is it possible to do it that way?

Alex

I’ll have to tag @kisvegabor on that one as I’m not sure exactly how the states in the themes work. Typically I just add styles on top of the theme.

I have succcesfully added and changed colors to the switch button when DISABLED, but I am unable to do the same for the label, I don’t know why

Alex

Hi,

It’d be great to see a more complete code snippet to reproduce the issue. I believe adding style to the screen wasn’t work becase there are some other objects between the screen and the label from where the label gets its color. E.g. screen->container->label. In this case the label’s style is inherited from the container.

To solve it on theme level you can add a disabled style to every label:

#if LV_USE_LABEL
        case LV_THEME_LABEL:
            list = lv_obj_get_style_list(obj, LV_LABEL_PART_MAIN);
            _lv_style_list_add_style(list, &styles->label);
            break;
#endif

What I have done until now:

In the material theme, whis labels get the color

COLOR_SCR_TEXT

which applies to the labels I want to modify. If I modify this color, the labels change. This color is applied in the basic section.

static void basic_init(void)
{
style_init_reset(&styles->scr);
lv_style_set_text_color(&styles->scr, LV_STATE_DEFAULT, COLOR_SCR_TEXT);
lv_style_set_value_color(&styles->scr, LV_STATE_DEFAULT, COLOR_SCR_TEXT);
lv_style_set_text_sel_color(&styles->scr, LV_STATE_DEFAULT, COLOR_SCR_TEXT);

then I have added the lines

lv_style_set_text_color(&styles->scr, LV_STATE_DISABLED, COLOR_SCR_TEXT_DIS);
lv_style_set_value_color(&styles->scr, LV_STATE_DISABLED, COLOR_SCR_TEXT_DIS);
lv_style_set_text_sel_color(&styles->scr, LV_STATE_DISABLED, COLOR_SCR_TEXT_DIS);

If the labels I have change with the COLOR_SCR_TEXT, should not change when they are disabled, adding the lines I have addded?

Alex

Hi!

I have added the section you posted, and also have added this:

static void label_init(void)
{
#if LV_USE_LABEL != 0
    style_init_reset(&styles->label);
    lv_style_set_text_color(&styles->label, LV_STATE_DEFAULT, COLOR_SCR_TEXT);
    lv_style_set_text_color(&styles->label, LV_STATE_DISABLED, COLOR_SCR_TEXT_DIS);
#endif
}

The only problem is that I have a list I use in other part of the code, and sometimes I disable a button of the list, with the line:

lv_btn_set_state(btn, LV_BTN_STATE_DISABLED);

I get the button pointer with:

lv_list_get_next_btn

The fact is that before the label changes, the button appeared as disabled, but now it does not show as disable (though you can not activate it). I don’t know why does that, because if it inheritated the label properties (instead of the ones of the list) it should show the disabled colors of the label.

Any clues?

Alex

Buttons do not inherit from labels, as far as I know. They inherit from containers.

What seems to have changed is the behaviour of the label inside the button of the list.

Before the change, whenever I disabled the button of the list, the label appeared disabled. But now that label is not shown as disabled.

I don’t know why is doing it, because both the label definition in the list, and the label definition out of the list, have now the LV_STATE_DISABLED color changed.

Alex

@abueno
Please send a short sample code to demonstrate the issue.

In a list, I do the following:

void listDisableElement(lv_obj_t *list, uint8_t id)
{
    lv_obj_t *btn;

    btn = listFindById(list, id);
    if (btn != NULL)
        lv_btn_set_state(btn, LV_BTN_STATE_DISABLED);

}

lv_obj_t *listFindById(lv_obj_t *list, uint8_t id) simply finds a button inside the list, with a Id, which is defined in the ext_attrib when adding the element.

When I set this btn inside the list to disabled, it is disabled (pressing does not affect it), and the text inside the list button appears greyed out.

If I add the following to the theme

static void label_init(void)
{
#if LV_USE_LABEL != 0
    style_init_reset(&styles->label);
    lv_style_set_text_color(&styles->label, LV_STATE_DEFAULT, COLOR_SCR_TEXT);
    lv_style_set_text_color(&styles->label, LV_STATE_DISABLED, COLOR_SCR_TEXT_DIS);
#endif
}

then the text inside the list button is not greyed out (but is disabled).

If instead of the previous I do

static void label_init(void)
{
#if LV_USE_LABEL != 0
    style_init_reset(&styles->label);
    lv_style_set_text_color(&styles->label, LV_STATE_DISABLED, COLOR_SCR_TEXT_DIS);
#endif
}

then the text appears grayed. I don’t know why the LV_STATE_DEFAULT style affects the button label when it is disabled.

Alex