Segment Control Example

This code places an Elementary segment control widgets on a window, to exemplify part of the widget's API.

Let's start adding a segment control to our window:

sc = elm_segment_control_add(win);
elm_segment_control_add
Evas_Object * elm_segment_control_add(Evas_Object *parent)
Add a new segment control widget to the given parent Elementary (container) object.
Definition elm_segment_control.c:646
evas_object_size_hint_weight_set(sc, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
elm_win_resize_object_add(win, sc);
evas_object_show(sc);
EVAS_HINT_EXPAND
#define EVAS_HINT_EXPAND
Use with evas_object_size_hint_weight_set(), evas_object_size_hint_weight_get(), evas_object_size_hin...
Definition Evas_Common.h:297
elm_win_resize_object_add
void elm_win_resize_object_add(Eo *obj, Evas_Object *subobj)
Add subobj as a resize object of window obj.
Definition efl_ui_win.c:9002
evas_object_show
EVAS_API void evas_object_show(Evas_Object *eo_obj)
Makes the given Evas object visible.
Definition evas_object_main.c:1814
evas_object_size_hint_weight_set
EVAS_API void evas_object_size_hint_weight_set(Evas_Object *obj, double x, double y)
Sets the hints for an object's weight.
Definition evas_object_main.c:2638

Now will add an item only with label:

elm_segment_control_item_add(sc, NULL, "only text");

Really simple. To add an item with only an icon, the icon needs to be created first, them added with this same function:

ic = elm_icon_add(win);
elm_icon_add
Evas_Object * elm_icon_add(Evas_Object *parent)
Add a new icon object to the parent.
Definition elm_icon.c:604
elm_icon_standard_set(ic, "close");
elm_segment_control_item_add(sc, ic, NULL);
elm_icon_standard_set
Eina_Bool elm_icon_standard_set(Evas_Object *obj, const char *name)
Set the icon by icon standards names.
Definition elm_icon.c:876

If an item with label and icon is required, it can be done as well. In this case, instead of a label (or icon) centered, the item will display an icon at left and the label at right:

ic = elm_icon_add(win);
elm_icon_standard_set(ic, "home");
elm_segment_control_item_add(sc, ic, "Home");

But, if you need to add some items that can have or not a label, but want that all of them looks the same way, with icon at left, just add an empty string label. It's done on our example to illustrate that:

ic = elm_icon_add(win);
elm_icon_standard_set(ic, "close");
elm_segment_control_item_add(sc, ic, "");

So far, all the item were added to the last position of the widget, but if something different is required, it can be done using another insertion function. Let's suppose we want to put an item just before the last item:

count = elm_segment_control_item_count_get(sc);
elm_segment_control_item_insert_at(sc, NULL, "Inserted at", count - 1);

There are two ways to delete items. Using the item handle, like:

seg_it = elm_segment_control_item_insert_at(sc, NULL, "To be deleted", 2);
elm_object_item_del(seg_it);
elm_object_item_del
void elm_object_item_del(Eo *obj)
Delete the given item.
Definition elm_main.c:2017

Or using item's index:

elm_segment_control_item_insert_at(sc, NULL, "To be deleted", 2);
elm_segment_control_item_del_at(sc, 2);

To set properties of an item already added to the widget, you just need to get the item and set icon or label, as the following code shows:

seg_it = elm_segment_control_item_get(sc, 0);

Finally, it's possible to select an item from the code, and also get the selected item. We will select the item at the center of the widget and print its position.

See the full example, whose window should look like this picture: