Radio buttons, an easy way to an easy developer?

Description

Implementing a radio button style interface

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

Several :slight_smile:

What do you want to achieve?

Get a one-shot selector element

What have you tried so far?

Wading through the docs trying to figure out which objects might be of use

Code to reproduce

None this far…

Hello!

One quite typical element in GUIs is a radio button style widget. Select language, pen width, motor type, sensor type - whatever!

In lv_settings app example there is a drop down list. This is of course one alternative especially if I could figure out the total length in pixels (item count * font height in pixels (how?!?)) so that the list would be of correct size right when opened - and even fix it open with lv_ddlist_set_stay_open.

But … a real radio button. Some layout on a container that sets texts to the left edge and button symbols (symbol font, image, maybe even lv_led?) to the right. A one-shot logic is a no-brainer, but I’d appreciate if someone could kick me into right direction selecting the most effective way to do this.

Thank you!

Some radio button examples:

image image

You can use the checkbox and restyle it to look more like a radio button. Then you can just handle the mutual exclusivity in code. When one checkbox gets checked, uncheck all the others.

1 Like

Oh dear!

How this has been slipped out of my eyes. Yes, got to have a look at this, thanks! Maybe I’m able to integrate this into lv_settings (those callbacks drive me feeling … eh, funny).

And one more: LvGL uses e.g in drop down list values like “item1\nitem2\nitem3”. Do you know by heart if there is a ready made string copy (or similar) to this kind of string where one could input the ordinal too? I have my 20-liner for this, but … you know. Every saved instruction is out of nightmares. And from map file too. (Application: in lv_settings I’d like to get the selected option in ddlist type).

I’m not entirely sure what you mean. Are you trying to make appending to the string simpler? If so, there is a new API (lv_dropdown_add_option) which allows you to add an option without having to compose the whole string with newlines by hand.

Yes, sorry, I could have been more specific.

I’m playing with the “lv_settings”, and there when a drop down selection is made, the value and state are provided. Say there is “One\nTwo\nThree” in a ddlist, and “Two” is selected: at callback I have the whole ddlist string as value and state is “1”. Now, I can have the selected contents like this:

idx_cpy(str_curr_item, act_item->value, act_item->state);

where the actual function is:

static void idx_cpy(char* dst, char* src, int idx)
{
    while (idx) {
        if (0 == *src)
            break;
        if ('\n' == *src)
            idx--;
        src++;
    }
    
    if (0 == *src)
        return;

    while (0 != *src && '\n' != *src) {
        *dst++ = *src++;
    }
    *dst = 0;
}

Of course I could replicate the ddlist items in a separate array and index that with the “state” - but once again, waste of memory :upside_down_face:

(Or then - I could of course have the values as normal zero ended strings and use those when building the ddlist)

Thanks!

Or just use the lv_ddlist_get_selected_str(ddlist, buf, buf_size) function which copies the text of the selected option to a buf .

1 Like

Thank you Sir, I’ll have a look at this too!