Radio example

In this example we will create 4 radio buttons, three of them in a group and another one not in the group. We will also have the radios in the group change the value of a variable directly and have then print it when the value changes. The fourth button is in the example just to make clear that radios outside the group don't affect the group.

We'll start with the usual includes:

#include <Elementary.h>

And move right to declaring a static variable(the one whose value the radios will change):

static int val = 1;

We now need to have a window and all that good stuff to be able to place our radios in:

static void _cb(void *data, Evas_Object *obj, void *event_info);
EAPI_MAIN int
elm_main(int argc, char **argv)
{
Evas_Object *win, *bx, *radio, *group, *ic;
elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
win = elm_win_util_standard_add("radio", "Radio");
elm_win_autodel_set(win, EINA_TRUE);
bx = elm_box_add(win);
elm_box_horizontal_set(bx, EINA_TRUE);
evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_show(bx);

And now we create a radio button, since this is the first button in our group we set the group to be the radio(so we can set the other radios in the same group). We also set the state value of this radio to 1 and the value pointer to val, since val is 1 this has the additional effect of setting the radio value to 1. For this radio we choose the default home icon:

group = radio = elm_radio_add(win);
elm_object_text_set(radio, "Radio 1");
ic = elm_icon_add(win);
elm_icon_standard_set(ic, "home");
elm_object_part_content_set(radio, "icon", ic);
elm_box_pack_end(bx, radio);
evas_object_size_hint_weight_set(radio, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(radio, EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_show(radio);

To check that our radio buttons are working we'll add a callback to the "changed" signal of the radio:

evas_object_smart_callback_add(radio, "changed", _cb, NULL);

The creation of our second radio button is almost identical, the 2 differences worth noting are, the value of this radio 2 and that we add this radio to the group of the first radio:

radio = elm_radio_add(win);
elm_object_text_set(radio, "Radio 2");
elm_radio_group_add(radio, group);
ic = elm_icon_add(win);
elm_icon_standard_set(ic, "file");
elm_object_part_content_set(radio, "icon", ic);
elm_box_pack_end(bx, radio);
evas_object_size_hint_weight_set(radio, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(radio, EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_show(radio);
evas_object_smart_callback_add(radio, "changed", _cb, NULL);

For our third callback we'll omit the icon and set the value to 3, we'll also add it to the group of the first radio:

radio = elm_radio_add(win);
elm_object_text_set(radio, "Radio 3");
elm_radio_group_add(radio, group);
elm_box_pack_end(bx, radio);
evas_object_size_hint_weight_set(radio, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(radio, EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_show(radio);
evas_object_smart_callback_add(radio, "changed", _cb, NULL);

Our fourth callback has a value of 4, no icon and most relevantly is not a member of the same group as the other radios:

radio = elm_radio_add(win);
elm_object_text_set(radio, "Radio 4");
elm_radio_group_add(radio, group);
elm_box_pack_end(bx, radio);
evas_object_size_hint_weight_set(radio, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(radio, EVAS_HINT_FILL, EVAS_HINT_FILL);
evas_object_show(radio);

We finally run the main loop:

evas_object_smart_callback_add(radio, "changed", _cb, NULL);
evas_object_show(win);
return 0;
}

And the last detail in our example is the callback that prints val so that we can see that the radios are indeed changing its value:

static void
_cb(void *data, Evas_Object *obj, void *event_info)
{
printf("val is now: %d\n", val);
}

The example will look like this:

radio_example_01.png