Insert a variable in lv_textarea

Insert a variable in lv_textarea

How to define a variable, insert it in an lv_textarea then retrieve the content.
For example if we define two variables x and y of type int. If we insert them in two lv_textarea, how to get the sum and display the result in a new textarea.
I will be very grateful to you because I am new to lvgl.

1 Like

You can use lv_textarea_get_text to get a string with the number, then you would need to use standard C functions to convert the string to an integer.

//create our textareas
lv_obj_t * ta_x = lv_textarea_create(scr, NULL);
lv_obj_t * ta_y = lv_textarea_create(scr, NULL);
lv_obj_t * sum_ta = lv_textarea_create(scr, NULL);

//here are our integers
int x = 5;
int y = 5;

//use a buffer and itoa to convert them, and set the textareas
char buffer [10];
itoa(x, buffer, 10);
lv_textarea_set_text(ta_x, buffer);
itoa(y, buffer, 10);
lv_textarea_set_text(ta_y, buffer);

//convert the textarea chars back to integers
int i = atoi(lv_textarea_get_text(ta_x));
int j = atoi(lv_textarea_get_text(ta_y));

//sum them
int sum = i + j;
//store in the buffer
itoa(sum, buffer, 10);
//write to the textarea
lv_textarea_set_text(sum_ta, buffer);

That said, this is fairly dangerous. If your user enters a non-integer (float, string, etc.) then this will fail immediately, so I wouldn’t do it myself unless you’re going to put checking in. Perhaps you should be looking at a spinbox

strtol supports error-checking, so your example could be adapted to use that if error-checking was necessary.

Bonjour, j’étais malade raison pour laquelle j’ai pas réagie à votre réponse. Je vous remercie d’avoir répondu. Je vais essayer votre exemple.

Hello dear, I tried your example and it works.
Currently I have five different text area. I will want to create a single keyboard for all the text area. Currently I can’t do it. Can you help me solve this problem?