(SOLVED) Lv_list_add_btn how to change text/symbol

lv_list_add_btn has icon and symbol support. It’s great. However, I want to change the icon or the text on the fly. On a already created item. It’s a bit to complicated for me to modify. It’s a class obj so it creates some random object which I don’t know the name of.

There was support for a load of stuff with regards to list_btn in 7.11, but V8 seems to have been nerfed allot.

Is there a way to do this? I’ve tried to remove the whole button and create it again, but that only adds it to the bottom. Very messy.

Ideas, code and pointers are greatly appreciated! :slight_smile:

EDIT: I managed to solve this issue myself, I think these functions I made should be added to the LVGL repo. First function adds a symbol to existing list button (if no img already exists). Second function can remove an image if it exists on a list button.

// ADD SYMBOL TO ALREADY EXISTING LIST BUTTON, IF NO IMG ALREADY EXISTS.
  lv_obj_t * lv_list_set_btn_symbol(lv_obj_t * list, lv_obj_t * btn, const char * icon)
  {
      LV_UNUSED(list);
      uint32_t i;
      int flag=0;
      Serial.println(lv_obj_get_child_cnt(btn));
      for(i = 0; i < lv_obj_get_child_cnt(btn); i++) {
        lv_obj_t * child = lv_obj_get_child(btn, i);
        if (lv_obj_check_type(child, &lv_img_class)) {
          flag++;
        }
      }

      if (flag == 0) {
        lv_obj_t * img = lv_img_create(btn);
        lv_obj_align(img, LV_ALIGN_RIGHT_MID, 0, 0);
        lv_img_set_src(img, icon);
      }
  }

// DELETE A SYMBOL FROM EXISTING LIST BUTTON
  lv_obj_t * lv_list_del_btn_symbol(lv_obj_t * list, lv_obj_t * btn)
  {
      LV_UNUSED(list);
      uint32_t i;
      for(i = 0; i < lv_obj_get_child_cnt(btn); i++) {
          lv_obj_t * child = lv_obj_get_child(btn, i);
          if(lv_obj_check_type(child, &lv_img_class)) {
            lv_obj_del(child);
          }
      }
  }

Hi.

Hi. I’ve solved such kind of problem for v7. Hope it will be useful.

/**
 * Set (replace existing) button's image
 * @param list pointer to a btn object
 * @param pointer to img_src
 * @return true - set OK, false - not set
 */
bool lv_list_set_btn_img(lv_obj_t * btn, const void * img_src)
{
	LV_ASSERT_OBJ(btn, "lv_btn");

#if LV_USE_IMG != 0

    lv_obj_t * img = lv_obj_get_child_back(btn, NULL);
    if(img == NULL) return false;

    while(lv_list_is_list_img(img) == false) {
        img = lv_obj_get_child_back(btn, img);
        if(img == NULL) break;
    }

    lv_img_set_src(img, img_src);
    lv_obj_invalidate(btn);

    return true;
#endif

    return false;
}

1 Like