Home » Android Switch Button Example – Kotlin

Android Switch Button Example – Kotlin

android kotlin switch button example

In this post, we are going to see the Android switch button example in Kotlin. Android Switch Button is a type of button that has two states namely ON and OFF. Switch Button can be used to perform an action instantaneously when the button is turned on. 

For example, Switch Button can be used to turn on or off specific settings instantly. The settings should apply instantly when the switch is turned on and off.

In the below Android switch button example, I have placed two switch buttons on the screen. When the user turns on or off any switch button a Toast message will be displayed saying whether the switch is on or off. and a button labeled “Reset” is placed at the bottom of the screen when the user press this button both the switch button state will be set to off.

To define a switch button we use <Switch> tag in the XML layout file.

Switch Button Example – Kotlin

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Switch
        android:id="@+id/switchButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Switch OFF"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <Switch
        android:id="@+id/switchButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="Switch OFF"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/switchButton1" />
    <Button
        android:id="@+id/buttonResetSwitch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        android:text="Reset"
        android:textColor="#FFFFFF"
        android:background="@color/colorPrimary"/>
</androidx.constraintlayout.widget.ConstraintLayout>

In the above XML code, I have defined two switch buttons and a button with unique ids.

MainActivity.kt

package com.example.switchbuttonexample
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        switchButton1.setOnCheckedChangeListener{_, isChecked ->
            if(isChecked) {
                switchButton1.text = "Switch ON"
                Toast.makeText(this, "First Switch Button: ON", Toast.LENGTH_LONG).show()
            }
            else {
                switchButton1.text = "Switch OFF"
                Toast.makeText(this, "First Switch Button: OFF", Toast.LENGTH_LONG).show()
            }
        }
        switchButton2.setOnCheckedChangeListener{_, isChecked ->
            if(isChecked) {
                switchButton2.text = "Switch ON"
                Toast.makeText(this, "Second Switch Button: ON", Toast.LENGTH_LONG).show()
            }
            else {
                switchButton2.text = "Switch OFF"
                Toast.makeText(this, "Second Switch Button: OFF", Toast.LENGTH_LONG).show()
            }
        }
        buttonResetSwitch.setOnClickListener{
            switchButton1.isChecked = false
            switchButton2.isChecked = false
        }
    }
}

Output:

android switch button example kotlin

Here in the above code, I used setOnCheckedChangeListener on the switch buttons to know the status of the switch whether its state is on or off. If the state of the button is on then switch text will be changed to “Switch ON” or else “Switch OFF”.

And I used setOnClickListener to get the click events of the Reset button. When the Reset button is clicked the switch buttons state will be set to off.

Leave a Reply

Your email address will not be published.