Align-center text on btnmatrix buttons


When a line break is in a button matrix text map item. The text is aligned as LV_LABEL_ALIGN_LEFT would align it for a label.
I would like it aligned as LV_LABEL_ALIGN_CENTER would align it for a label.

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

TFT Simulator

What do you want to achieve?

To have the symbols centralized above the text. Please see screenshot.

What have you tried so far?

Nothing yet, no idea how to.

Code to reproduce

    lv_obj_t* scr = lv_scr_act();
    lv_obj_t* btnmatrix = lv_btnmatrix_create(scr, NULL);

    static const char* const btnmatrix_map[] = {
        LV_SYMBOL_LOOP"\nManual Run",
        LV_SYMBOL_PLUS"\nNew Test",
        LV_SYMBOL_REFRESH"\nCurrent Test",
        LV_SYMBOL_MINUS"\nPrevious Test",

    lv_btnmatrix_set_map(btnmatrix, (const char**)btnmatrix_map);
    lv_obj_set_width(btnmatrix, lv_obj_get_width(scr));
    lv_obj_set_height(btnmatrix, LV_DPI * 3 / 4);
    lv_obj_align(btnmatrix, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, 0);

Screenshot and/or video

@kisvegabor, After looking to see how this is done on the widgets I,

  • Added a 2 bit align member to lv_btnmatrix_ext_t as it is done for other widgets,
  • Added lv_btnmatrix_get_align and lv_btnmatrix_set_align functions,
  • Added code to set the requried alignment in lv_btnmatrix_design.

This way the alignment applies to the entire matrix as it is not made up of labels like most (if not AFAIK all) of them that have alignment get/set functions…

That seemed to be the best way of achieving the above.

Now, I am not sure if you think it is worth extending the API to include button matrix text alignment which is why I have not created a PR ? If so, I will do a PR.

If not kindly let me know how else this could be done as I would prefer to use the unmodified library.


It’s really not supported now but your solution seems great! A PR would be welcome! :slight_smile:

:+1: Thanks, will do !