×

SwitchComponent

The SwitchComponent is an additional option for toggling between two states or values. It can be used as an alternative for the CheckBox. It comprises a mandatory title, an optional value and help text. The SwitchComponent can be disabled, in which case, it is displayed faded. When it is used to change the status of an Object which could take a long time to complete, the SwitchComponent can display an intermediate loading indicator while performing the change.

Figure 1. A switch with a title

The following snippet initializes a switch with a title only:

Display.init(this);
Form form = new Form("Switch Demo");
simpleSwitch = new SwitchComponent("Flight mode", null, null, false);
form.addComponent(simpleSwitch);
form.show();

If we need to listen for any changes in the SwitchComponent in order to modify the value, we need to set a SwitchComponentListener. The following snippet demonstrates how to construct a SwitchComponent with a help text that listens for status changes and modifies the SwitchComponent’s value accordingly:

Display.init(this);
Form form = new Form("Switch Demo");
switchWithValuesAndHelp = new SwitchComponent(
    "Flight mode", "Off", "Turns off all phone connections",
    false);
switchWithValuesAndHelp.setSwitchComponentListener(new SwitchComponentListener() {
    public void notifySwitchListener(SwitchComponent switchComponent) {
        if (switchComponent == switchWithValuesAndHelp) {
            if (switchComponent.isSwitchOn()) {
                switchComponent.setSwitchOn(true);
                switchComponent.setValue("On");
            }
            else {
                switchComponent.setSwitchOn(false);
                switchComponent.setValue("Off");
            }
        }
    }
});

form.addComponent(switchWithValuesAndHelp);
form.show();

Figure 2. SwitchComponent with title, value and help text

Note:

The method isSwitchOn() is called after the user has tapped the SwitchComponent. When the SwitchComponentListener triggers the notifySwitchListener call back method, the switch will already have changed its status. This means that when the switch is Off and there is a change of status, the method isSwitchOn() will return true, because it is executed after the switch has changed to On.

The SwitchComponent has a third, waiting status. The waiting status can be activated from the SwitchComponent itself and then dismissed by an external event, such as the completion of a network Thread.

Figure 3. SwitchComponent in Waiting status

When using the waiting status, the method isSwitchOn will return the last known state before the switch entered the waiting status and not the target state, as it happens with the simple SwitchComponent case. In the code snippet below, the SwitchComponent will enter into the waiting state every time it is toggled between On and Off. Triggering the exit from the wait state is simulated here by pressing the Command assigned to the Toolbar:

Display.init(this);
Form form = new Form("Switch Demo");

stopCommand = new Command("Stop");
form.addCommand(stopCommand);
delayedSwitch= new SwitchComponent(
    "Flight mode", "Off", "Turns off all phone connections",
    true);
form.addCommandListener( new ActionListener() {

    public void actionPerformed(ActionEvent event) {
        if (event.getCommand() == stopCommand) {
            if (delayedSwitch.isWaiting()) {
                if (delayedSwitch.isSwitchOn()) {
                    delayedSwitch.setSwitchOn(false);
                    delayedSwitch.setValue("Off");
                }
                else {
                    delayedSwitch.setSwitchOn(true);
                    delayedSwitch.setValue("On");
                }
                delayedSwitch.finishWaiting();
            }
        }
    }
});
form.addComponent(delayedSwitch);
form.show();

Last updated 29 July 2013

Back to top

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×