In this tutorial we will learn about how to implement a toggle button in android, how to set OnCheckedChangeListener() for toggle button and show a toast when toggle button is clicked and their example .

Toggle Button

Toggle Button is a kind of button which is used when you have 2 very specific options and the user should be able to switch between these option . Toggle button is normally used to switch between two different settings .Toggle button have two different states checked(ON) and Unchecked(OFF) .

1. Create a New Project :

Lets we start with a new project in Android Studio from File New Project ⇒
select Empty Activity and go through it.

2. Implement toggle button in android xml layout :

Add Toggle Button to your XML Layout file i.e. activity_main.xml to implement set OnCheckedChangeListener() to display it for better user experience or just add the following code to activity_main.xml file .

    <ToggleButton
        android:id="@+id/toggleBtn"
        android:layout_centerInParent="true"
        android:textOff="OFF"
        android:textOn="ON"
        android:checked="false"
        android:layout_marginBottom="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <ToggleButton
        android:id="@+id/toggleBtn"
        android:layout_centerInParent="true"
        android:textOff="OFF"
        android:textOn="ON"
        android:checked="false"
        android:layout_marginBottom="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

3. Implement OnCheckedChangeListener to handle the event of Toggle Button :

Now set OnCheckedChangeListener to handle the event of Toggle Button to perform an action or event on switch between different options and show a toast message in MainActivity.java file .

toggleBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    // The toggle is enabled
                    Toast.makeText(MainActivity.this,"Toggle is Enabled ",Toast.LENGTH_SHORT).show();

                } else {
                    // The toggle is disabled
                    Toast.makeText(MainActivity.this,"Toggle is Disabled ",Toast.LENGTH_SHORT).show();
                }
            }
        });

MainActivity.java :

package net.technxt;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;


public class MainActivity extends AppCompatActivity {

    ToggleButton toggleBtn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toggleBtn = (ToggleButton)findViewById(R.id.toggleBtn);

        toggleBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    // The toggle is enabled
                    Toast.makeText(MainActivity.this,"Toggle is Enabled ",Toast.LENGTH_SHORT).show();

                } else {
                    // The toggle is disabled
                    Toast.makeText(MainActivity.this,"Toggle is Disabled ",Toast.LENGTH_SHORT).show();
                }
            }
        });


    }


}

Run the application to launch Android emulator or in your android device to test toggle button in android and verify the result of the changes done in the application.

%d bloggers like this: