How to change text color in a table

Description

I have a table with different text/values and want to use color on the text and/or the cell.

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

Simulator version 7.0

What do you want to achieve?

I would like to color the text inside a table cell.

What have you tried so far?

I have tried the method used with the labels, when the option “recolor” is on, but it does not seem to work for tables.

I know that changing styles I could do something about it, but I don’t know if there is a simpler way. The styling method is still a little obscure to me…

Alex

Hi @abueno,

By default all the cells in the table header row are assigned to LV_TABLE_PART_CELL2 style set and all the cells in the body of the table are assigned to LV_TABLE_PART_CELL1 style set.

So to change the colour of all the cells in the header row, for example, you would do this:

 lv_obj_set_style_local_text_color(table, LV_TABLE_PART_CELL2, LV_STATE_DEFAULT, LV_COLOR_BLUE);

If you want to colour a particular cell you can do this:

lv_obj_set_style_local_text_color(table, LV_TABLE_PART_CELL3, LV_STATE_DEFAULT, LV_COLOR_RED);
lv_table_set_cell_type(table, row, col, LV_TABLE_PART_CELL3);

I hope that helps.

Kind Regards,

Pete

1 Like

This would do, but by now I am using 3 styles, (top row, left column and rest of the table) and need to put two different colors in the cells, but I have only one CELL STYLE left…

I think I will have to limit the styles to be able to use the 2 different colors… or are there any more options?

Thank you

Alex

Hi @abueno,

I am not aware of any other way to do this but I can ask @kisvegabor or @embeddedt if they have any input here?

I have no idea what level you work at but another option might be to edit the lv_table.h file in your lvgl library sources to add some more options: (Don’t forget to rebuild the library if you do this.)

There is a define at line 35: #define LV_TABLE_CELL_STYLE_CNT 4

and an enum at line 68:

/*Parts of the table*/
enum {
    LV_TABLE_PART_BG,
    LV_TABLE_PART_CELL1,
    LV_TABLE_PART_CELL2,
    LV_TABLE_PART_CELL3,
    LV_TABLE_PART_CELL4,
};

It looks like you can just increase the value LV_TABLE_CELL_STYLE_CNT and add the appropriate members to the enum.

Kind Regards,

Pete

Yes, I have seen the define, thanks. I don’t like to change the original libraries, because then I have to remember to edit every time I upgrade the code, but it’s on my list of options.

Thanks for the help. If anyone has any other idea…

aLeX

Hi Alex,

Yes I can relate to that!

Hopefully there is another work around I am not aware of.

Kind Regards,

Pete

The best way to do it is probably to just add support for the recoloring flag. It’s very simple and doesn’t really add anything to the code size (recoloring is implemented at the rendering level, not the object level).

This is how I did it for the button matrix. At first glance it should mainly involve adding similar logic here in lv_table_design (as well as the appropriate getters/setters for the feature).

2 Likes

Thanks for the prompt reply @embeddedt. :slight_smile:

I would be willing to take a look at this but I am probably unable to do it for a week or so as I am a little pushed at the moment.

Is it something you might want to have a go at yourself @abueno, if you need it quickly?

Kind Regards,

Pete

Thanks for the hints. It’s a good idea to implement the option and integrate in the code. At the moment I am very busy moving all the graphic system of my old code to the new one using lvgl, and I can’t do it right now. I have used the STYLE option, and it works for me. If I have time I will do it in the future.

Thanks!

Alex

2 Likes