ToggleButton in Kotlin

06 Jan, 2022
In Android, ToggleButton is just like a switch containing two states either ON or OFF which is represented using boolean values true and false respectively. ToggleButton unlike switch does not have a slider interface i.e we cannot slide to change the states. It is just like a button. In this article, we will be discussing how to create a ToggleButton in Kotlin.
Note: ToggleButton inherits the button class of android. Therefore, all the attributes of the button are also applicable here.
Following are some of the additional important attributes available along ToggleButton
To create a new project in Android Studio follow these steps:
Click on File, then New and then New Project and give name whatever you like.Choose “Empty Activity” for the project template.Then, select Kotlin language Support and click next button.Select minimum SDK, whatever you need
Click on File, then New and then New Project and give name whatever you like.
Choose “Empty Activity” for the project template.
Then, select Kotlin language Support and click next button.
Select minimum SDK, whatever you need
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">      <ToggleButton        android:id="@+id/toggleButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>
The toggle button in the layout can be accessed using the findViewById() function.
val toggle: ToggleButton = findViewById(R.id.toggleButton)
After accessing set a listener to perform actions based on the toggle state using setOnCheckedChangeListener() method.
toggle.setOnCheckedChangeListener { _, isChecked -> **Perform Any Action Here**}
Java
package com.example.togglebuttonsampleimport androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.widget.Toastimport android.widget.ToggleButton  class MainActivity : AppCompatActivity() {      override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)          val toggle: ToggleButton = findViewById(R.id.toggleButton)        toggle.setOnCheckedChangeListener { _, isChecked ->            Toast.makeText(this, if(isChecked) "Geek Mode ON" else "Geek Mode OFF", Toast.LENGTH_SHORT).show()        }    }}
sooda367
Android-Button
Kotlin Android
Picked
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Add Views Dynamically and Store Data in Arraylist in Android?
Android SDK and it's Components
How to Communicate Between Fragments in Android?
Flutter - Custom Bottom Navigation Bar
Retrofit with Kotlin Coroutine in Android
How to Add Views Dynamically and Store Data in Arraylist in Android?
Android UI Layouts
How to Communicate Between Fragments in Android?
Kotlin Array
Retrofit with Kotlin Coroutine in Android