ToggleButton in android – What-is and how-to-use.

In this post, you will learn about the ToggleButton in android. You will learn what a toggle button is and how to use it in an android app.

What is the ToggleButton in android?

A ToggleButton in Android is a button that can be either ON or OFF i.e; a ToggleButton has two states – ON and OFF. You can toggle between the two states by tapping on the button. ToggleButton is often used in app settings where the user can enable or disable certain features. For example, suppose you are building an application that needs to provide a “night mode” setting. In that case, you could use a ToggleButton. When the ToggleButton is in the “on” state, the night mode setting would be activated, and when in the “off” state, the night mode setting would be disabled. The ToggleButton is displayed in its original form when it is off. When the ToggleButton is active, the button is displayed in another form.

Android 4.0 (API level 14) introduces switch that is an another kind of toggle button that provides a slider control.

Now, let me show you a visual example of the ToggleButton.

Now, let’s see the code to create the ToggleButton in android. I will share with you a simple app that contains a toggle button. When I tap on the toggle button it shows a toast message.

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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ToggleButton
        android:id="@+id/toggleButton"
        android:layout_width="169dp"
        android:layout_height="66dp"
        android:layout_marginTop="364dp"
        android:text="ToggleButton"
        android:textSize="36sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.549"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.togglebutton_example;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

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

        ToggleButton toggleButton=(ToggleButton)findViewById(R.id.toggleButton);
        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isToggleChecked) {

                if(isToggleChecked){
                    Toast.makeText(MainActivity.this, "You just turned on the toggle button", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this, "You just turned off the toggle button", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }
}

Output:

That’s it for this post.

Goodbye.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: