For textareas, how to set the cursor location and scroll to top

What do you want to achieve?

Create a textarea, and put text in it (text longer than one screen).
Then, go to the top of the textarea:
scroll to the top of the textarea, and set the cursor at the top of the textarea.

Solution:

It took me a while to figure out that both of these API calls are needed,
to go to the top of the textarea:
lv_textarea_set_cursor_pos(screen_textarea, 0);
lv_obj_scroll_to_y(screen_textarea, 0, LV_ANIM_ON);

The textarea web-page describes moving the cursor via lv_textarea_set_cursor_pos().
But, doesn’t describe scrolling via lv_obj_scroll_to_y()
https://docs.lvgl.io/9.5/widgets/textarea.html

I’m posting this info for anyone else who’s trying to figure this out.

Note that lv_textarea_set_text() automatically scrolls to the bottom of the screen, and it moves the cursor there too (I believe).
So, going to the top of the textarea should be done after calling lv_textarea_set_text().

Environment

  • MCU/MPU/Board: Cheap Yellow Display
  • LVGL version:: 9.3

Code:

screen_textarea = lv_textarea_create(lv_screen_active());
lv_obj_align(screen_textarea, LV_ALIGN_TOP_MID, 0, 0);
lv_obj_set_size(screen_textarea, 320, 205);
lv_textarea_set_cursor_click_pos(screen_textarea, true);
lv_obj_add_state(screen_textarea, LV_STATE_FOCUSED); // To be sure the cursor is visible

lv_textarea_set_text(screen_textarea, screen_text_ptr);

lv_textarea_set_cursor_pos(screen_textarea, 0);
lv_obj_scroll_to_y(screen_textarea, 0, LV_ANIM_ON);

1 Like

Thanks @JimYuill
This is certainly helpful

1 Like