How to set a manual position of an object inside a cell grid?

Description

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

LVGL simulator, VS Code on Linux, Platformio, emulator_64bits

What LVGL version are you using?

LVGL v9

What do you want to achieve?

I want to set the x, y position of the text inside the labels (e.g. the “First Name” label) to fix a distance beween the label and the textarea on its bottom side. I want to achieve this for all the labels in the grid.
I know there are already the lv_obj_set_grid_cell() functions and I’m using them, however I can’t understand how to manually set the labels positions, with margins in pixels in the cell.

What have you tried so far?

I tried to use the llv_obj_set_y() and lv_obj_set_x() and lv_obj_set_pos() on the p_label object but it still remains in the same position.

Code to reproduce

This is my current code.

#include "BasicLayout.h"
#include "lvgl.h"
#include <stdio.h>
#include <stdint.h>

// We want to buid a generic form where the user insert data.
// We won't use a flexbox because the upper part will be different from the
// bottom part, so a grid will fit better for this.
//
void
layout_demo1 (void)
{
    // We use a fixed size because we want some margin from the borders.
    //
    static int32_t col_dsc[] = {350, 350, LV_GRID_TEMPLATE_LAST};
    static int32_t row_dsc[] = {32, 24, 24, 24, 24, 24, 24,
                                LV_GRID_TEMPLATE_LAST};

    lv_obj_t * p_screen(lv_screen_active());
    lv_obj_set_grid_dsc_array(p_screen, col_dsc, row_dsc);

    // This won't make so much difference because we take all the available
    // screen.
    //
    lv_obj_set_grid_align(p_screen, LV_GRID_ALIGN_SPACE_AROUND,
                          LV_GRID_ALIGN_START);

    // Element on the top.
    //
    lv_obj_t * p_label(lv_label_create(p_screen));
    lv_label_set_text(p_label, "Community Service Hours Form");
    lv_obj_set_grid_cell(p_label, LV_GRID_ALIGN_CENTER, 0, 2,
                         LV_GRID_ALIGN_CENTER, 0, 1);

    // First form line.
    //
    p_label = lv_label_create(p_screen);
    lv_label_set_text(p_label, "First Name");
    lv_obj_set_grid_cell(p_label, LV_GRID_ALIGN_START, 0, 1,
                         LV_GRID_ALIGN_END, 1, 1);

    p_label = lv_label_create(p_screen);
    lv_label_set_text(p_label, "Last Name");
    lv_obj_set_grid_cell(p_label, LV_GRID_ALIGN_START, 1, 1,
                         LV_GRID_ALIGN_END, 1, 1);

    // Second form line.
    //
    lv_obj_t * p_first_name_txt(lv_textarea_create(p_screen));
    lv_textarea_set_one_line(p_first_name_txt, true);
    lv_textarea_set_placeholder_text(p_first_name_txt, "First Name");

    // The stretch permits to take the entire column.
    //
    lv_obj_set_grid_cell(p_first_name_txt, LV_GRID_ALIGN_STRETCH, 0, 1,
                         LV_GRID_ALIGN_CENTER, 2, 1);

    lv_obj_t * p_last_name_txt(lv_textarea_create(p_screen));
    lv_textarea_set_one_line(p_last_name_txt, true);
    lv_textarea_set_placeholder_text(p_last_name_txt, "Last Name");
    lv_obj_set_grid_cell(p_last_name_txt, LV_GRID_ALIGN_STRETCH, 1, 1,
                         LV_GRID_ALIGN_CENTER, 2, 1);

    // Third form line.
    //
    p_label = lv_label_create(p_screen);
    lv_label_set_text(p_label, "Card Number");
    lv_obj_set_grid_cell(p_label, LV_GRID_ALIGN_START, 0, 1,
                         LV_GRID_ALIGN_END, 3, 1);

    p_label = lv_label_create(p_screen);
    lv_label_set_text(p_label, "Number of Hours Assigned");
    lv_obj_set_grid_cell(p_label, LV_GRID_ALIGN_START, 1, 1,
                         LV_GRID_ALIGN_END, 3, 1);

    // Fourth form line.
    //
    lv_obj_t * p_card_num_txt(lv_textarea_create(p_screen));
    lv_textarea_set_one_line(p_card_num_txt, true);
    lv_textarea_set_placeholder_text(p_card_num_txt, "Card Number");
    lv_obj_set_grid_cell(p_card_num_txt, LV_GRID_ALIGN_STRETCH, 0, 1,
                         LV_GRID_ALIGN_CENTER, 4, 1);

    lv_obj_t * p_hours_assigned_txt(lv_textarea_create(p_screen));
    lv_textarea_set_one_line(p_hours_assigned_txt, true);
    lv_textarea_set_placeholder_text(p_hours_assigned_txt,
                                     "Number of Hours Assigned");
    lv_obj_set_grid_cell(p_hours_assigned_txt, LV_GRID_ALIGN_STRETCH, 1, 1,
                         LV_GRID_ALIGN_CENTER, 4, 1);

    // Fifth form line.
    //
    p_label = lv_label_create(p_screen);
    lv_label_set_text(p_label, "Placement Agency");
    lv_obj_set_grid_cell(p_label, LV_GRID_ALIGN_START, 0, 2,
                         LV_GRID_ALIGN_END, 5, 1);

    // Sixth form line.
    //
    lv_obj_t * p_placement_ag_txt(lv_textarea_create(p_screen));
    lv_textarea_set_one_line(p_placement_ag_txt, true);
    lv_textarea_set_placeholder_text(p_placement_ag_txt, "Placement Agency");

    // We need a stretch on the column to be able to take the entire two cols.
    //
    lv_obj_set_grid_cell(p_placement_ag_txt, LV_GRID_ALIGN_STRETCH, 0, 2,
                         LV_GRID_ALIGN_CENTER, 6, 1);
}   /* layout_demo1() */

If I understand correctly you want some distance between the label and the textarea? Shouldn’t lv_obj_set_style_pad_bottom(p_label, x, LV_PART_MAIN ); do the trick?

Yep, that’s it!
Thank you for your help, I’m still learning and I didn’t learn the styles yet :grin:

1 Like