Skip to main content

Posts

Showing posts with the label Switch

android kotlin - Switch button listener

MainActivity.kt package com.example.jetpack import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // text to reverse val text = "ANDROID DEVELOPERS" // switch button checked change listener switchButton.setOnCheckedChangeListener { compoundButton, b -> if (b){ // when switch button is checked textView.text = "Switch is on." textView.append("\n\n${text.reversed()}") }else{ // if switch button is unchecked textView.text = "Switch is off." textView.append("\n\n$text") } } } } activity_ma...

android kotlin - Switch button example

Switch Button The following kotlin tutorial will demonstrate to us how we can use a Switch button in an android application. And how we can implement the Switch button checked change listener using the kotlin programming language. We used the SwicthCompat widget in this tutorial instead the Switch/SwitchMaterial widget. Android Developers also can use the SwitchMaterial widget instead the Switch/SwicthCompat widget. The SwitchMaterial widget used attributes from the material theme. The SwitchMaterial behaves identically to the SwitchCompat widget. The SwicthCompat is a complete backport of the core Switch widget. The SwitchCompat widget brings the visuals and functionality of the Switch widget to older versions of the android platform. We have to put the SwicthCompat widget in our XML layout file to use it instead Switch button in an android application. We have to explicitly use the SwicthCompat widget. Android application developers can change the Switc...