---
title: "Toggle Button In Android"  
description: "•	It is basically a checked/unchecked states when we generated an on/off event.  •	It is beneficial when the user change between two states.  •	It pro"  
author: "Nitish Kesarwani"  
published: 2017-12-23  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/12741/toggle-button-in-android  
category: "android apps"  
tags: ["android", "android activity", "android controls"]  
reading_time: 4 minutes  

---

# Toggle Button In Android

Toggle Button Description

• It is basically a checked/unchecked state when we generated an on/off event.

• It is beneficial when the user change between two states.

• It produces flashlight indicator when the state is on while off states nothing show such kind of behaviour

• It is generally used in Wifi, [Bluetooth](https://www.mindstick.com/blog/11658/all-you-need-to-know-about-bluetooth-5), and [Developers](https://yourviews.mindstick.com/view/88480/why-developers-combine-multiple-ai-tools-instead-of-one) option enabled in a phone

• In Android 4.0, it is available in more modified ways. It is used as the switch on/off button

• Android [ToggleButton](https://www.mindstick.com/forum/12937/how-to-style-togglebutton-so-that-the-background-color-is-white) and switch both are the subclasses of a compound button

• In [XML file](https://www.mindstick.com/articles/75/how-to-read-and-write-xml-file-through-c-sharp), you have to declare texton and text off [attributes](https://www.mindstick.com/articles/13105/2-attributes-that-make-your-odoo-ecommerce-theme-productive-and-engaging) which [response](https://yourviews.mindstick.com/view/86896/biden-s-assertive-response-to-special-counsel-critique-on-memory-strength) when the [button is clicked](https://answers.mindstick.com/qa/94813/why-does-my-app-terminate-when-the-button-is-clicked-on-android-studio)

• I have declared two ToggleButton and one Button, TextView. Here, button is used to get the response on submitting the on/off status of ToggleButton

• [StringBuilder](https://www.mindstick.com/blog/99/stringbuilder-class-in-c-sharp) is used to bind the string and display into the TextView

• toggleStatusEnabled method is response onCLicListener method with onClick method

##### Attributes

• **android: textOn:** Its value is displayed when button status is on

**• android:textOff:** Its value is invoked when button status is off

**• android: checked:** If the value is true, the [default value](https://www.mindstick.com/forum/23023/default-value-when-using-singleordefault) is checked to be set

##### Methods

**• drawableStateChanged():** The method is called when change of view state occurs then it reflect impact on the drawable

**• finishInflator():** Finalize the inflator view state of XML

**• textOn()** ,**textOff()** and **checked()** are the method which is similar to the attributes defined in it

\

[AndroidManifest.xml](https://www.mindstick.com/interview/12768/what-is-the-androidmanifest-xml)

```
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mapsample.msclient009.togglebutton">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
```

\

Activity_Main.xml\

```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="com.mapsample.msclient009.togglebutton.MainActivity"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <ToggleButton
        android:id="@+id/tg1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Bluetooth"
        android:textOff="No"
        android:textOn="yes"/>

    <ToggleButton
        android:id="@+id/tg2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="WiFi"
        android:textOff="No"
        android:textOn="yes"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Status" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tv"/>
    </LinearLayout>
</LinearLayout>
```

**\**

```
Main_Activity.javapackage com.mapsample.msclient009.togglebutton;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {
Button btn;
ToggleButton tbtn1,tbtn2;
TextView txt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );
        toggleStatusEnabled();
    }
    public void toggleStatusEnabled()
    {btn =(Button)findViewById ( R.id.btn );
    tbtn1 =(ToggleButton)findViewById ( R.id.tg1 );
    tbtn2 = (ToggleButton)findViewById (R.id.tg2 ) ;
    txt =(TextView)findViewById (R.id.tv );
        btn.setOnClickListener ( new View.OnClickListener ( ) {
        @Override
        public void onClick(View v) {
            StringBuilder builder = new StringBuilder ( );
            builder.append ( "First Toggle Button is Respond " ).append (tbtn1.getText());
            builder.append ("\nSecond Toggle Button is Respond ").append (tbtn2.getText());
            txt.setText ( builder );
        }
    } );

    }
}
```

##### Launch Your APP

![Toggle Button In Android](https://www.mindstick.com/mindstickarticle/fd1cfe70-01be-45b2-bb3c-2327e26caef2/images/f06a5a95-b53e-4e56-b0b6-81a28e020dea.png)![Toggle Button In Android](https://www.mindstick.com/mindstickarticle/fd1cfe70-01be-45b2-bb3c-2327e26caef2/images/dbb6b547-e859-4d31-9052-01b6e719a867.png)

\

---

Original Source: https://www.mindstick.com/articles/12741/toggle-button-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
