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.

https://docs.lvgl.io/master/widgets/roller.html?highlight=roller#add-fade-mask-to-roller

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

https://docs.lvgl.io/master/widgets/dropdown.html#simple-drop-down-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

"0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n41\n42\n43\n44\n45\n46\n47\n48\n49\n50\n51\n52\n53\n54\n55\n56\n57\n58\n59\n60\n61\n62\n63\n64\n65\n66\n67\n68\n69\n70\n71\n72\n73\n74\n75\n76\n77\n78\n79\n80\n81\n82\n83\n84\n85\n86\n87\n88\n89\n90\n91\n92\n93\n94\n95\n96\n97\n98\n99\n100\x00"

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 ?

Thanks!

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