Sunday, September 8, 2019

Android Kotlin SwitchCompat (on/off) Button

A Switch is a two-state toggle switch widget that can select between two options. The user may drag the "thumb" back and forth to choose the selected option, or simply tap to toggle


SwitchCompat is a version of the Switch widget which runs on devices back to API 7.

Android Switch and ToggleButton both are the subclasses of CompoundButton class.

Lets see a sample example .

 XML Layout

  1. Create XML Layout activity_main.xml , this will be set as content view for launcher Activity (MainActivity.kt)
  2. Added Seekbar To the xml layout .
file : activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity"
android:padding="30dp"
android:orientation="vertical"
android:id="@+id/root">

<androidx.appcompat.widget.SwitchCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Swtich Button"
android:id="@+id/switchCompat01"/>

</LinearLayout>

setOnCheckedChangeListener For Switch Button


setOnCheckedChangeListener is a callback that notifies swtich button is on /off

switchCompat01.setOnCheckedChangeListener { buttonView, isChecked ->
val msg: String = if (isChecked)
"Switch Button is Checked"
else
"Switch Button is UnChecked"
showToast(msg)
}

Activity


  1. Create a koltin class MainActivity.kt and extend it to AppCompatActivity class .
  2. Override onCreate function and set the content of this MainActivity with above defined xml layout (activity_main.xml).
  3. Set the setOnCheckedChangeListener for swtich and show the status  .

file : MainActivity.kt
package com.tutorialsbuzz.androidswtich

import android.os.Bundle
import android.widget.Toast
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)

switchCompat01.setOnCheckedChangeListener { buttonView, isChecked ->
val msg: String = if (isChecked)
"Switch Button is Checked"
else
"Switch Button is UnChecked"
showToast(msg)
}
}

private fun showToast(msg: String) {
Toast.makeText(this@MainActivity, msg, Toast.LENGTH_SHORT).show()
}
}






If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website

Related Posts:

  • SQL WHERE ClauseThe WHERE clause is used to filter records. The WHERE clause is used to extract only those records that fulfill a specified criterion. SQL WHERE Syn… Read More
  • SQL UNION OperatorThe SQL UNION Operator The UNION operator is used to combine the result-set of two or more SELECT statements. Notice that each SELECT statement with… Read More
  • SQL SELECT INTO StatementThe SQL SELECT INTO statement can be used to create backup copies of tables. The SQL SELECT INTO StatementThe SELECT INTO statement selects data from … Read More
  • SQL ORDER BY KeywordThe ORDER BY KeywordThe ORDER BY keyword is used to sort the result-set by a specified column.The ORDER BY keyword sort the records in ascending order… Read More
  • SQL AND & OR OperatorsThe AND & OR operators are used to filter records based on more than one condition. The AND & OR OperatorsThe AND operator displays a record… Read More

2 comments: