ToggleButton in Kotlin

ToggleButton works similarly to a switch, with two states: ON or OFF, represented by the boolean values true and false, respectively. ToggleButton, unlike switch, does not offer a slider interface, so we cannot slide to change states. It is exactly like a button. In this tutorial, we will go over how to develop a ToggleButton in Kotlin of Mobile application.

Anroid ToggleButton XML Attributes
  • id : The id assigned to the toggle button
  • textOff: The text is shown on the button when it is not checked.
  • texton: The text is shown on the button when it is checked.
  • disabledAlpha: The alpha to apply to the when disabled.
Syntax
ToggleButton XML:
 <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" />
ToggleButton kt file :
   val toggle: ToggleButton = findViewById(R.id.toggleButton)
        toggle.setOnCheckedChangeListener { _, isChecked ->
            Toast.makeText(this, if(isChecked) " ON" else "OFF", Toast.LENGTH_SHORT).show()
        }


Create a new project in Android studio
  1. To create a new project in Android studio follow these steps:
  2. Click on file and select the option like open project or create new project.
  3. Click new project and select "empty activity"  , select the Kotlin option.
 Adding below code in main_activity.xml of project 
 <?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">
<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>
 Adding below code in MainActivity.kt of project 
 import android.os.Bundle
import android.widget.Toast
import android.widget.ToggleButton
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
val toggle: ToggleButton = findViewById(R.id.toggleButton)
toggle.setOnCheckedChangeListener { _, isChecked ->
Toast.makeText(
this,
if (isChecked) " Mode ON" else " Mode OFF",
Toast.LENGTH_SHORT
).show()
}
}
}
Output of above example 

                                                     


Happy coding!

365Bloggy July 2, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive