Software Development Exam  >  Software Development Notes  >  ToggleButton in Kotlin

ToggleButton in Kotlin - Software Development PDF Download

ToggleButton in Kotlin

Last Updated : 06 Jan, 2022

In Android, a ToggleButton functions like a switch with two states: ON or OFF, represented by boolean values true and false. Unlike a switch, a ToggleButton does not have a slider interface; instead, it resembles a button. Let's delve into creating a ToggleButton in Kotlin.

ToggleButton

Note: ToggleButton inherits attributes from the Android button class.

Note:

Below are some key additional attributes available for ToggleButton:

AttributeDescription
android:idThe ID assigned to the toggle button
android:textOffThe text displayed on the button when it is not checked
android:textOnThe text displayed on the button when it is checked
android:disabledAlphaThe opacity applied when the button is disabled

To create a new project in Android Studio, follow these steps:

  • Click on File, then New, and then New Project and provide a name for your project.
  • Choose "Empty Activity" as the project template.
  • Next, select Kotlin language support and click the next button.
  • Select the minimum SDK as per your requirements.

XML Layout

The ToggleButton in the layout can be accessed using the findViewById() function.

Understanding Toggle Button in Android

  • Toggle buttons in Android are a type of button that has two states: ON and OFF.
  • They are used to switch between two states or to turn a feature on or off in an application.

Implementation in Android Studio

  • After locating the toggle button in the layout using findViewById(), a listener is set using setOnCheckedChangeListener() to perform actions based on the toggle state.
  • The setOnCheckedChangeListener() method is used to detect changes in the state of the toggle button.

Example Code Snippet

package com.example.togglebuttonsampleimport androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.widget.Toastimport android.widget.ToggleButtonclass 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() } }}

Explanation

  • The code snippet demonstrates the implementation of a toggle button in an Android application.
  • When the toggle button's state changes, a Toast message is displayed based on whether the button is checked or not.
  • For example, if the toggle button is checked, the message "Geek Mode ON" is displayed; otherwise, "Geek Mode OFF" is shown.
ToggleButton in Kotlin - Software Development

Please Login to comment...

LoginLike
The document ToggleButton in Kotlin - Software Development is a part of Software Development category.
All you need of Software Development at this link: Software Development
Download as PDF

Top Courses for Software Development

Related Searches

study material

,

ToggleButton in Kotlin - Software Development

,

Viva Questions

,

past year papers

,

Exam

,

Semester Notes

,

ToggleButton in Kotlin - Software Development

,

Objective type Questions

,

ppt

,

Important questions

,

pdf

,

Sample Paper

,

MCQs

,

Free

,

mock tests for examination

,

video lectures

,

Summary

,

Previous Year Questions with Solutions

,

ToggleButton in Kotlin - Software Development

,

Extra Questions

,

shortcuts and tricks

,

practice quizzes

;