Dynamic autofit V8


I try to Dynamically adapt the heigh of a object according to the size of not hidden childrens.

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


What LVGL version are you using?


What do you want to achieve?

I want the heigh of the object adapts when I hidde or display a child.

What have you tried so far?

I tryed using lv_obj_set_height(cont, LV_SIZE_CONTENT);

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:

lv_obj_t *cont = lv_obj_create(lv_scr_act());
lv_obj_set_width(cont, 200);
lv_obj_set_height(cont, LV_SIZE_CONTENT);
lv_obj_align(cont, LV_ALIGN_TOP_MID, 0, 20);

lv_obj_t *label;

lv_obj_t *btn1 = lv_btn_create(cont);
lv_obj_align(btn1, LV_ALIGN_TOP_MID, 0, 10);
label = lv_label_create(btn1);
lv_label_set_text(label, "Button");

lv_obj_t *btn2 = lv_btn_create(cont);
label = lv_label_create(btn2);
lv_label_set_text(label, "Toggled");
lv_obj_align_to(btn2, btn1, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);

    if((millis()/1000) % 2)
      lv_obj_add_flag(btn2, LV_OBJ_FLAG_HIDDEN);
      lv_obj_clear_flag(btn2, LV_OBJ_FLAG_HIDDEN);

LV_ALIGN_OUT_BOTTOM_MID doesn’t work well with LV_SIZE_CONTENT because it would cause a circular dependency: the bottom position of cont depends on btn2, and btn2 should be aligned to the bottom of cont.
Try using e.g.

lv_obj_align(btn2, LV_ALIGN_TOP_MID, 0, 100);

thanks for your reply

I tried it, but cont is not resized :frowning:

I gave a simple example to show the behavior.
but my real need is several buttons and dropdown which should be display or hidden according to user settings, so a fixed position would not be compatible.

in V7 I was able to do it with lv_btn_set_fit2(btn2, LV_FIT_PARENT, LV_FIT_TIGHT); // autorise l'adaptation dynamique but I got fit2 was not available anymore in V8

An other observation :
in my global project, I have different screens, when I switch to an other screen and come back to this one, the size is adapted

Just an idea.
Change you code to toggle LV_OBJ_FLAG_HIDDEN only ONCE every 0.5 sec. and also call
lv_obj_invalidate( cont ) once right after to have “cont” redrawn by lv_task_handler().