MainActivity.kt
package com.cfsuman.kotlintutorials
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.switchmaterial.SwitchMaterial
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// get the widgets reference from XML layout
val switchMaterial = findViewById<SwitchMaterial>(R.id.switchMaterial)
val textView = findViewById<TextView>(R.id.textView)
// get the switch button initial on off status
if (switchMaterial.isChecked)
{
textView.text = "Switch is initially on."
}else{
textView.text = "Switch is initially off."
}
// switch button checked change listener
switchMaterial.setOnCheckedChangeListener { compoundButton, b ->
if (b){
// when checked
textView.text = "Switch is on."
}else{
// if unchecked
textView.text = "Switch is off."
}
}
}
}
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"
android:background="#DCDCDC"
android:id="@+id/rootLayout"
android:padding="24dp">
<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/switchMaterial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="On Off Me"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.25" />
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:fontFamily="sans-serif"
tools:text="TextView"
android:gravity="center"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/switchMaterial" />
</androidx.constraintlayout.widget.ConstraintLayout>
build.gradle dependencies[add]
// Material components
implementation 'com.google.android.material:material:1.6.1'
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTHimF53bTJcG3c0FwlUw_3OY1DdbFj0TCJ6NbHQEueI-fJSwDuFqhh8J9Sjmoy_GbrknDcTGJn0apt7dmf1IHwOnUVf2CyBXuuvNc9qYElM2nn1MP8Ne278J-8FjhtB4cndYiTZUvFTci0TkGB-nm_3ebZhkHgIp0KHXb3hhZtffhr3aivx2sy99aSQ/s1600/android_kotlin_material_switch_button_example.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4e6UoPijwr9wzVosIAfdLmslQJDq-VzJ-8FqlOAW3OvTnM8YgAMKWM1_2zfuUhMozkQujCbe-GdEUU-xwWgeygsOMeZzq5IC4N0y0NI17tEzwgLNFm8-VylvmudSB1u2saEITnPSNOujofhB5qagXXkyZ3z9doTVcvNLaZfoEENV3xEH8Wf3tgv8N8g/s1600/android_kotlin_material_switch_button_example2.png)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimyxCZDkEII3laCBwVObb0xivc_E7zjhdJTisVkJoyMU8OFnVNbbvFnAGSninJ6gMCo-tNQmHeHturp9FAjcqJE8blxN67Ra9-zBJKJPkZ_K2vW4AkKE1hBqXLXUhKmDsGPJ_mkgIpYoWsYnfkOVKO4cKbdlyuh-4I6q5VF3RtmJud-twJcoqP0UmAuA/s1600/android_kotlin_material_switch_button_example3.png)
- android kotlin - Switch button listener
- android kotlin - Get raw resource uri
- android kotlin - Button with icon
- android kotlin - Resize ImageView programmatically
- android kotlin - ImageView circle crop
- android kotlin - Circular ImageView with border
- android kotlin - Circular ImageView programmatically
- android kotlin - ImageView border radius
- android kotlin - ImageView add border programmatically
- android kotlin - ImageView add border
- android kotlin - ImageView rounded corners programmatically
- android kotlin - ImageView set image from drawable
- android kotlin - ImageView set image from url
- android kotlin - Get battery level programmatically
- android kotlin - Get battery voltage programmatically