Sunday, February 11, 2018

android kotlin - Switch button example

MainActivity.kt

package com.cfsuman.kotlinexamples

import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Set an checked change listener for switch button
        switch_button.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                // The switch is enabled/checked
                text_view.text = "Switch on"

                // Change the app background color
                root_layout.setBackgroundColor(Color.GREEN)
            } else {
                // The switch is disabled
                text_view.text = "Switch off"

                // Set the app background color to light gray
                root_layout.setBackgroundColor(Color.LTGRAY)
            }
        }


        // Set a click listener for root layout object
        root_layout.setOnClickListener{
            // Get the switch button state programmatically
            if(switch_button.isChecked){
                // If switch button is checked/on then
                // The switch is enabled/checked
                text_view.text = "Switch is on"

                // Change the app background color
                root_layout.setBackgroundColor(Color.GREEN)
            }else{
                // The switch is unchecked
                text_view.text = "Switch is off"

                // Set the app background color to light gray
                root_layout.setBackgroundColor(Color.LTGRAY)
            }
        }


        // Set a click listener for the button widget
        button.setOnClickListener{
            // Change the switch button checked state on button click
            switch_button.isChecked = if(switch_button.isChecked)false else true
        }
    }
}
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:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    >
    <Switch
        android:id="@+id/switch_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="25dp"
        />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change Switch Button State"
        />
    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
        android:layout_marginTop="50dp"
        />
</LinearLayout>