Dropdown list of integers

Hi LVGL community,
I want to create a dropdown list of integers, I imagined a generic function like this:

/* create a list of integers with step (for temperature) */
 lv_dropdown_int_create(parent, min, step, max);

I spent a lot of time trying to create it but without success.

Any help please ?

I think lv_roller would be better suited for this kind of thing.


But if you really want a drop down kind of a list.


The same problem, I have to write the list manually.
so to have a list from 0 to 100, I need to write all of this values.

What i want is something like :

const char *opts[max + 1];
char tmp[12];
for (int i = 0; i <= max; i++) {
     sprintf(tmp, "%d", i);
     opts[i] = malloc(sizeof(tmp) + 1);
     strcpy(opts[i], tmp);

roller = lv_roller_create(lv_scr_act());
 lv_roller_set_options(roller, opts, LV_ROLLER_MODE_NORMAL);

but this does not work for me.

you cannot do what you are doing. You are not adding a newline character at the end of each number and you are not using the correct position in your strcpy for opts. the position in opts is not going to be the same as the for loop

numbers 0 - 9 only have a single character (10)
numbers 10 - 99 have 2 characters (89 * 2)
100 has 3 characters
and a newline at the end of each totals 101 characters. 0-100 is 101 items.

when you get to number 100 the position in opts should be 288 not 100. the position of 288 comes from 1 char for numbers 0 - 9 and 2 chars for numbers 10 - 99. so far we have 10 + (89 * 2) which is 188. then there are 100 newline characters at the end of each number from 0 to 99 so that adds an additional 100 characters which brings the total to 288.

There is only allowed to be a single null in the entire string and that is at the very end.

1 Like

and here you go


Thanks a lot, It seems the correct solution.
(I tested for numbers 0 - 9 and it worked fine)

If you still have the code that worked for you, can you post it here please ?


Here is my code for generating a list of numbers between 0 to 99.

lv_obj_t *sr_create_dropdown_int(lv_obj_t *parent, uint16_t min,
	 uint16_t step, uint16_t max)
	lv_obj_t *dropdown = NULL;
	uint16_t opts_len = ((max - min) / step) + 1;
    uint16_t opts_size = opts_len * 4;
    char opts[opts_size];
    uint16_t n = 0;

	memset(opts, 0, opts_size*sizeof(char));

	for (int i = min; i < max; i += step) {
		n += sprintf(&opts[n], "%d\n", i);
    sprintf(&opts[n], "%d", max);

	/* create the dropdown widget*/
	dropdown = lv_dropdown_create(parent, NULL);
    lv_dropdown_set_options(dropdown, opts);
	return dropdown;

Thanks for @kdschlosser

so you got it working… fantastic!!

as I said I am not that proficient in C code but at least I was able to point you in the right direction to get it working properly, that’s the important thing.

It’s also good that you were able to hammer it out yourself because you will remember how to do it in the future. Someone handing you the code is not always the best thing because you don’t learn that way.

1 Like