Android ToggleButton with Example in Kotlin

Android ToggleButton uses at User Interface, where the user has can change the setting between two states. Some example of use On/Off – Volume, Wifi, Bluetooth, etc. Since Android 4.0, Android introduces another type of toggle button called switch, that provides slider control.

Android ToggleButton and Switch are the subclasses of CompoundButton class.

Android ToggleButton with Example in Kotlin

Android ToggleButton Attributes

Here are the some important attribute of ToggleButton in Android.


The ID which uniquely identifies the layout. Required when activity class needs to get state of it.


This is the alpha to apply to the indicator when disabled.


When Toggle Button is not checked , then this mentioned text will show to user.


This is the text for the button when it is checked.

Note: If you need to change a button’s state yourself, you can use the CompoundButton.setChecked()

Let’s Build a Complete Example of Android ToggleButton :

In this example you learn basic use of ToggleButton in kotlin language

Step 1. Create new project “Build Your First Android App in Kotlin
Step 2. Add below code in “activity_main.xml” resource file

add Toggle button in the resource file and add id “toggleButton“.

Step 3. Open the “MainActivity.kt” and add following code

set setOnCheckedChangeListener on Toggle button and check current state

Step 4. Now Run the application, in emulator or On you android device

Output screenshot Android ToggleButton example :

Android ToggleButton with Example in Kotlin output

Download source code Android Toggle Button in kotlin

Do comment if you have any doubt and suggestion on this tutorial.

Note: This example (Project) is developed in Android Studio 3.1. Tested on Android 9 ( Android-P), compile SDK version API 27: Android 8.0 (Oreo)



Coding in Kotlin

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.