Open In App
Related Articles

How to add Toggle Button in an Android Application

Improve Article
Save Article
Like Article

ToggleButton is basically a stop / play or on/off button with indicator light indicating the current state of ToggleButton. ToggleButton is widely used, some examples are on/off audio, Bluetooth, WiFi, hot-spot etc. This is a subclass of Composite Button.

ToggleButton allows users to change settings between two states from your phone’s Settings menu such as turning your WiFi, Bluetooth, etc. on / off. Since the Android 4.0 version (API level 14), it has another type of toggle button called switch which provides user slider control.

Programmatically, isChecked() method is used to check the current state of the toggle button. This method returns a boolean value. If a toggle button is ON, this returns true otherwise it returns false. Below is the example in which toggle button is used.


  • Step 1: Create a new project and fill all the required details for the app like the app name, package name, etc.

    Select File -> New -> New Project -> fill required details and click “Finish”

  • Step 2: In this step, open the XML file and add the code to display the toggle button and a textview.

    res -> Layout -> Activity_Main.xml (or) Main.xml




  • Step 3: In this step, open MainActivity and add the below code to initialize the toggle button and add onToggleClick method which will be invoked when the user clicks on the toggle button. This method changes the text in textview.

    Open the app -> Java -> Package ->


    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.ToggleButton;
    public class MainActivity
        extends AppCompatActivity {
        ToggleButton togglebutton;
        TextView textview;
        protected void onCreate(
            Bundle savedInstanceState)
                = (ToggleButton)findViewById(
                = (TextView)findViewById(
        public void onToggleClick(View view)
            if (togglebutton.isChecked()) {
                textview.setText("Toggle is ON");
            else {
                textview.setText("Toggle is OFF");



Now connect your device with USB cable and launch the application. You will see a toggle button. Click on the toggle button which will display the status of the toggle button.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 19 Feb, 2021
Like Article
Save Article
Similar Reads
Complete Tutorials