All TalkersCode Topics

Follow TalkersCode On Social Media

Toggle Button In Android

Last Updated : Mar 2, 2023

Toggle Button In Android

In this article we will show you the solution of toggle button in android, you can use the checked-or-unchecked (On/Off) status of the button to be shown on an Android toggle button.

It is advantageous if the user must switch the configuration between two states. Since Android 4.0, there has been a second category of toggle button known as switch, which offers slider control.

There are two subclasses of CompoundButton on Android: ToggleButton and Switch.

ToggleButton functions as a pause, play, or on/off button with an indicator light that shows the button's status at any given time.

There are several applications for toggle buttons, including turning on or off Bluetooth, WiFi, and hotspots. This belongs to the Composite Button subclass.

From the Settings menu on your phone, users can utilise ToggleButton to switch between two states for settings like turning WiFi, Bluetooth, etc. on or off.

With the introduction of Android 4.0 (API level 14), a different kind of toggle button known as a switch that offers user slider control was included.

In programming, the toggle button's status is checked using the isChecked() method.

The use of a toggle button is demonstrated here.

Use the Android ToggleButton object's Touch action to interact with a toggle button, which TestComplete links to that control.

With this operation, the control can be controlled with just one quick touch.

Using the ToggleButton in the settings menu, you can toggle between two states like turning on or off WiFi, Bluetooth, etc.

An additional toggle button that can be controlled by the user appears in Android 4.0 ( API level 14). The Switch is a type of toggle button that enables the user to control a slider.

With a ToggleButton, you can toggle off/on and get an indication of the toggle button's status by using a light indicator.

A ToggleButton can turn on or off different elements on a device, such as sound, Bluetooth, WiFi, hotspot, etc. The compoundButton class subclasses this one.

By using the isChecked() method, we can programmatically check whether a toggle button is checked or not.

Boolean values of true or false are returned by this method. An active toggle button returns true if it is selected, otherwise it returns false if it is un-selected.

An example of the code to check a toggle button's status is shown below.

Toggle buttons have an attribute called Checked that indicates their current state. Toggling buttons should have a value of true or false, where true represents the checked state and zero represents the unchecked state.

The checked attribute has the value false by default. A programmatic approach can also be taken to set the current state.

function Test()
  var p = Mobile.Device().Process("com.example.myapp");
  var Toggle = p.RootLayout("").Layout("layoutTop").Layout("layout1").ToggleButton("toggle1");
  1. First we have to define function.
  2. Then we have to select the device for creating the button.
  3. Toggle buttons should be created next.
  4. Then we create variables for the processing program.
  5. Then we create variables as root layout for the program.
  6. Then we touch the toggle button and post the current toggle button state to the log.

Conclusion :-

In Android, ToggleButton is used to display the checked and unchecked state of a button. ToggleButton is basically an off/on button with a light indicator which indicates the current state of the toggle button.

The most simple example of ToggleButton is doing on/off in sound, Bluetooth, wifi, hotspot etc.

It is a subclass of compoundButton. To check the current state of a toggle button programmatically we use isChecked() method. This method returns a Boolean value either true or false.

If a toggle button is checked then it returns true otherwise it returns false.

Below is the code which checks the current state of a toggle button.

I hope this article on toggle button in android helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials