
Toggle Button Using Kotlin
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical"> <ToggleButton android:id="@+id/toggleButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="ToggleButton" /> </LinearLayout> |
MainActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.androindian.togglekotlin 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) toggleButton.setOnCheckedChangeListener { buttonView, isChecked -> if(isChecked==false){ Toast.makeText(this@MainActivity,"TOFF",Toast.LENGTH_LONG).show() }else{ Toast.makeText(this@MainActivity,"TON",Toast.LENGTH_LONG).show() } } } } |