How to make empty image object not say "No data", be a color other than white

Description

When creating an image object, the background is always white with the text “No data” in the top left corner.
I want to change the background to black, and get rid of the text.

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

N/A

What LVGL version are you using?

v7

What do you want to achieve?

I want to make image objects that are empty (no actual image data) to appear differently than they do.

What have you tried so far?

Set the background color to black (See below).

Code to reproduce

		lv_obj_t * img = lv_img_create(lv_scr_act(), NULL);
		lv_obj_set_style_local_bg_color(img, LV_IMG_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);

Screenshot and/or video

image

You need to call lv_img_set_src() to set an lv_img_dsc_t object to the image object.

I suspect that the background color is hardcoded at the moment. I will need to look into whether it’s possible to use the background color from the styles.

It’s really hardcoded now but the problem is that the drawing functions don’t see the object itself, only areas and properties to draw. Hence the image drawer can’t see the image object to get its bg color.

It would need to be forwarded from lv_img_design.

At the very least, an empty image object should show nothing. Then at least a plain object with the same size can be used as the background.
Right now, white + text is not ideal, especially hard coded.

I took this approach from HTML where you get a “broken image symbol” when the image is not available.
image

My reasoning was if the image can’t be opened, you need to fix it anyway. So it’s better to show an explicit error.

What about showing nothing if src=NULL and “No data” on error?

For my purpose, I “fixed” this by using a black object behind the image, and set the image size to 0x0 pixels when there is no data.
This way, the image area looks like a black box when there is no image data, then when there is image data I align it to the center of the black object, so there is still a black background.
Basically I wanted an image placeholder even when there is no data (the image data comes in live over the air and can change at any time/not yet exist). So I have essentially solved that problem but in a roundabout way.
In general I am not a fan of things like the hard coded “No\rData” or even “Text” as the default text for a label… wastes code space, etc. when you are just going to change it anyway (and if you want it blank, you have to explicitly set it to a blank string). But that is just my opinion…

@upbeat27
Just out of curiosity, what is your use case when the image is not available immediately?

Sorry for the late reply - the images are received “over the air”, so I have a placeholder until they are received.

If you are thinking of images included in the UI design itself, then generally there should be an image available. But if you think of, say, an image viewer application developed in LVGL, using an LVGL image object to display a user selected image, then there are plenty of use cases where the image is not “available immediately”, like the user hasn’t selected one, uploaded it, etc., like this: :slight_smile:

2 Likes

Very nice UI!

Thanks, but the real credit lies to the power and flexibility of LVGL!! I don’t think there’s any other way I could have done this on a Teensy 4.1.