NativeScript Core

In this article
API Reference
Not finding the help you need?

Segmented Bar

Using a SegmentedBar inside JavaScript project gives you a simple way to define a collection of tabbed views. The SegmentedBar’s selectedIndexChange property notifies you for every changes of the component’s selectedIndex.

The specific SegmentedBar properties are:

  • items
  • selectedIndex
  • selectedIndexChange
const segmentedBarModule = require("tns-core-modules/ui/segmented-bar");

Basics

The example shows how to create SegmentedBar component via XML and handle selectedIndex change.

<SegmentedBar row="0"  class="m-5" selectedIndex="{{ sbSelectedIndex }}">
    <SegmentedBar.items>
        <SegmentedBarItem title="Item 1" />
        <SegmentedBarItem title="Item 2" />
        <SegmentedBarItem title="Item 3" />
    </SegmentedBar.items>
</SegmentedBar>

Improve this document

Demo Source


Code Behind

In the following example is shown, how to create SegmentedBar via Code-behind

<StackLayout id="stackLayoutId">
    <Label text="{{ 'Result(index): ' + sbResult}}" textWrap="true" />

</StackLayout>

Improve this document

Demo Source


Views

Example showing how to handle the SegmentedBar index change event and changing the displayed Layouts.

<SegmentedBar row="0" loaded="sbLoaded" class="m-5" selectedIndex="{{ sbSelectedIndex }}">
    <SegmentedBar.items>
        <SegmentedBarItem title="Item 1" />
        <SegmentedBarItem title="Item 2" />
        <SegmentedBarItem title="Item 3" />
    </SegmentedBar.items>
</SegmentedBar>
<GridLayout row="1" rows="*" visibility="{{ visibility1 ? 'visible' : 'collapsed' }}" backgroundColor="white">
    <Label text="{{ 'The new selectedIndex is: ' + prop }}" class="m-15 h3 p-5 text-center"/>
</GridLayout>

<GridLayout row="1" rows="*" visibility="{{ visibility2 ? 'visible' : 'collapsed' }}" backgroundColor="green">
    <Label text="{{ 'The new selectedIndex is: ' + prop }}" class="m-15 h3 p-5 text-center" color="white"/>
</GridLayout>

<GridLayout row="1" rows="*" visibility="{{ visibility3 ? 'visible' : 'collapsed' }}" backgroundColor="red">
    <Label text="{{ 'The new selectedIndex is: ' + prop }}" class="m-15 h3 p-5 text-center" color="white"/>
</GridLayout>

Improve this document

Demo Source


API Reference for the SegmentedBar Class

Native Component

Android iOS
android.widget.TabHost UISegmentedControl