Saturday, February 10, 2018

android kotlin - ToggleButton example

ToggleButton
The following kotlin tutorial will demonstrate to us how we can use a ToggleButton in an android application. And how we can implement the ToggleButton checked change listener using the kotlin programming language.

We used the standard ToggleButton widget in this tutorial instead the AppCompatToggleButton widget. The AppCompatToggleButton is a ToggleButton that supports compatible features on older versions of the android platform.

Android application developers can tint the ToggleButton background color and drawable color. We can tint both the background and drawable color using API methods and XML attributes. Developers can display an icon on the ToggleButton surface in different positions.

App developers can set the ToggleButton’s on and off states text using both XML attributes and API methods. Developers also can initially check or uncheck (On or Off) a ToggleButton using both XML attribute and API method.

The ToggleButton OnCheckedChangeListener registers a callback to be invoked when the ToggleButton checked state is changed. So when users change the ToggleButton checked state, the developers can get the change in real time using this listener. They can perform the required action when the ToggleButton is checked and unchecked.

Android developers also can get the ToggleButton checked and unchecked state (On and Off state) programmatically using the ‘isChecked’ property. Even the developers can change the ToggleButton checked state programmatically using this ‘isChecked’ property/method.

This android kotlin tutorial code is written in an android studio IDE. Copy the code and paste it into your android studio IDE and run it on an emulator to see how we use a ToggleButton in an android application.

And how we implemented the ToggleButton checked change listener and how we get the ToggleButton checked status programmatically in an application. We displayed screenshots of the emulator screen, which also help you to understand the code without running it on an android device or emulator.
MainActivity.kt

package com.cfsuman.kotlintutorials

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout


class MainActivity : AppCompatActivity() {
    private lateinit var context: Context

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

        // Get the context
        context = this;

        // Get the widgets from XML layout
        val rootLayout = findViewById<ConstraintLayout>(R.id.rootLayout);
        val button = findViewById<Button>(R.id.button);
        val toggleButton = findViewById<ToggleButton>(R.id.toggleButton);

        // initially set the layout background color
        rootLayout.setBackgroundColor(
            Color.parseColor(
                if (toggleButton.isChecked)"#8FBC8F" else "#C2B280")
        )

        // Set an checked change listener for toggle button
        toggleButton.setOnCheckedChangeListener {buttonView, isChecked ->
            if (isChecked) {
                // The toggle is enabled/checked
                showToast("Toggle on")
                // Change the app background color
                rootLayout.setBackgroundColor(
                    Color.parseColor("#8FBC8F")
                )
            } else {
                // The toggle is disabled
                showToast("Toggle off")
                // Set the app background color
                rootLayout.setBackgroundColor(
                    Color.parseColor("#C2B280")
                )
            }
        }

        // Set a click listener for root layout object
        rootLayout.setOnClickListener{
            // Get the toggle button state programmatically
            if(toggleButton.isChecked){
                // If toggle button is checked/on then
                // The toggle is enabled/checked
                showToast("Toggle on")
            }else{
                // The toggle is unchecked
                showToast("Toggle off")
            }
        }

        // Set a click listener for the button widget
        button.setOnClickListener{
            // Change the toggle button checked state on button click
            toggleButton.isChecked = !toggleButton.isChecked
        }
    }

    // function to show toast message
    private fun showToast(message:String){
        Toast.makeText(context,message,Toast.LENGTH_SHORT).show()
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rootLayout"
    android:padding="24dp">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change Toggle State"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ToggleButton
        android:id="@+id/toggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>
More android examples