Android ToggleButton with Example in Kotlin

Android ToggleButton uses at User Interface, where the user has can change the setting between two states. Some examples of use On/Off – Volume, Wifi, Bluetooth, etc. Since Android 4.0, Android introduces another type of toggle button called switch, which 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.

android:id

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

android:disabledAlpha

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

android:textOff

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

android:textOn

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“.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ToggleButton
        android:id="@+id/toggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:text="ToggleButton"
        android:disabledAlpha="0.1"
        android:textOn="Turn On"
        android:textOff="Turn Off"/>

</RelativeLayout>
Step 3. Open the “MainActivity.kt” and add following code

set setOnCheckedChangeListener on Toggle button and check current state

package `in`.eyehunt.androidtogglebutton

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
import android.widget.CompoundButton
import android.widget.Toast


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        toggleButton.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                Toast.makeText(this,"Turned On",Toast.LENGTH_LONG).show()
            } else {
                Toast.makeText(this,"Turned Off",Toast.LENGTH_LONG).show()
            }
        })
    }
}
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

https://github.com/EyeHunts/AndroidToggleButton

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)

MinSdkVersion=”15″

TargetSdkVersion=”27″

Coding in Kotlin


Leave a Reply

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