Open In App

How to add Toggle Button in an Android Application

ToggleButton is basically a stop / play or on/off button with indicator light indicating the current state of ToggleButton. ToggleButton is widely used, some examples are on/off audio, Bluetooth, WiFi, hot-spot etc. This is a subclass of Composite Button.

ToggleButton allows users to change settings between two states from your phone’s Settings menu such as turning your WiFi, Bluetooth, etc. on / off. Since the Android 4.0 version (API level 14), it has another type of toggle button called switch which provides user slider control.



Programmatically, isChecked() method is used to check the current state of the toggle button. This method returns a boolean value. If a toggle button is ON, this returns true otherwise it returns false. Below is the example in which toggle button is used.

Approach



Output:

Now connect your device with USB cable and launch the application. You will see a toggle button. Click on the toggle button which will display the status of the toggle button.


Article Tags :