Skip to main content

Set Toggle

The Set Toggle action enables or disables a toggle or switch using its displayed label. This action allows administrators to control switch-based settings within an application without manually interacting with the device. Unlike Set Checkbox, which is intended for checkbox controls, Set Toggle is used for UI elements represented as switches or toggles.

When to Use​

Use this action when you want to:

  • Enable or disable Wi-Fi from Android Settings.
  • Turn Bluetooth on or off.
  • Enable or disable application settings represented by switches.
  • Automate the configuration of device or application preferences.

Syntax​

var task = suremdmjs.automate().toggle(
"<Package Name>",
"<Label Text>",
<Enabled>
);

suremdmjs.automate().performAction(
task,
"onSuccess",
"onFailure"
);

Parameters​

ParameterRequiredDescription
Package NameYesPackage name of the target application. Leave empty ("") to search all applications.
Label TextYesVisible text displayed next to the target toggle or switch.
EnabledYesPass true to enable the toggle, or false to disable it.

Parameter Details​

Enabled

ValueDescription
trueEnables the specified toggle or switch.
falseDisables the specified toggle or switch.

Example​

The following example enables the Wi-Fi toggle from Android Settings.

!#suremdmjs

enableWifi();

function enableWifi() {

var task = suremdmjs.automate().toggle(
"com.android.settings",
"Wi-Fi",
true
);

suremdmjs.automate().performAction(
task,
"wifiEnabled",
"toggleFailed"
);
}

function wifiEnabled() {
suremdmjs.log("Wi-Fi enabled successfully.");
}

function toggleFailed() {
suremdmjs.log("Wi-Fi toggle could not be located or enabled.");
}

How This Example Works​

  1. Searches for the Wi-Fi toggle within the Android Settings application.
  2. Identifies the toggle using its displayed label.
  3. Uses the Enabled parameter set to true to enable Wi-Fi.
  4. Executes the success callback if the toggle action succeeds.
  5. Executes the failure callback if the toggle cannot be located or the action fails.

Expected Result​

The Wi-Fi toggle is enabled automatically. If the action cannot be completed, the failure callback is executed.

note

The target toggle must be accessible through the device's Accessibility Service. The displayed label must match the intended toggle or switch. Some system settings may restrict automation depending on the Android version and device configuration.

💬 Help us improve this documentation

Was this information useful?

Your feedback helps us keep our documentation accurate, up to date, and useful.