Android ToggleButton

We will learn about the Toggle Button widget of Android in this tutorial. We'll also learn about the various toggle widget attributes that are used to configure this widget.

Android Toggle Button widget can be defined as below –

"Android Toggle button is a widget that are used to display checked/unchecked state as button with light indicator and The ON or OFF text is accompanied by default. And It is subclass of Compound button."



Attributes in ToggleButton are –

S. No. XML Attributes Description
1 android:disabledAlpha This is value of alpha you want to set when indicator is disabled.
2 android:textOff  Text to be shown when toggle button is in OFF state.
3 android:textOn Text to be shown when toggle button is in ON state.

Attributes in ToggleButton are also inherited from TextView and Compound Button. Some of the popular attributes inherited from TextView are –

S. No. XML Attributes Description
1 android:id This is unique id of the ToggleButton to uniquely identify the ToggleButton.
2 android:height Sets height of the ToggleButton.
3 android:width Sets width of the ToggleButton.
4 android:text Sets the text of the ToggleButton.
5 android:textAllCaps Shows text in capital letters
6 android:textColor Sets color of the text.
7 android:textSize Sets size of the text.
8 android:textStyle Sets style of the text. For example, bold, italic etc.
9 android:typeface Sets typeface of the text. For example, normal, sans, monospace etc.
10 android:clickable Sets true when you want to make this View clickable. Otherwise, set false.
11 android:background Sets background to this View.
12 android:backgroundTint Sets tint to the background.
13 android:drawableBottom This is drawable to be drawn at bottom of the text.
14 android:drawableEnd This is drawable to be drawn to end of the text.
15 android:drawableLeft This is drawable to be drawn to left of the text.
16 android:drawablePadding This is padding of the drawable.
17 android:drawableRight This is drawable to be drawn to right of the text.
18 android:drawableStart This is drawable to be drawn to start of the text.
19 android:drawableTop This is drawable to be drawn at top of the text.

Attributes in ToggleButton inherited from Compound Button are –

S. No. XML Attributes  Description
1 android:button Drawable used for the button graphic (for example, checkbox and radio button).
2 android:buttonTint Tint to apply to the button graphic.

Note: – Visit android official documentation for more details about attributes of ToggleButton in android.

To create a new android project follow the steps below. If you have already developed the project. Please ignore the steps.

S. No. Steps
1 Open Android Studio.
2 Go to File => New => New Project. Write application name as ToggleButton. Then, click next button.
3 Select minimum SDK you need. However, we have selected 14 as minimum SDK. Then, click next button
4 Then, select Empty Activity => click next => click finish.
5 If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit tutorial to Create a New Project to know steps in detail.

Open res/values/strings.xml file. Then, add below code into it.

<string name="app_name">ToggleButton</string> 
<string name="togglebutton">ToggleButton</string> 

Open res/layout/activity_main.xml file. Then, add below code into it

<?xml version="1.0" encoding="utf-8"?>
tools:context=".MainActivity"> <ToggleButton 


Here, we have defined ToggleButton in xml file. Now, we will access this ToggleButton in java file and show a toast message when it is checked on/off.

Open src/main/java/com.ukacademe.togglebutton/ file. Then, add below code into it.

package com.ukacademe.togglebutton;

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

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        ToggleButton toggleButton = findViewById(;
        if (toggleButton != null) {
            toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    String msg = "Toggle Button is " + (isChecked ? "ON" : "OFF");
                    Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();


Since AndroidManifest.xml is very importatnt file for any android application . We are also going to mention it here.



Code inside main/AndroidManifest.xml file is as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.ukacademe.togglebutton"
<application android:allowBackup="true"
<activity android:name=".MainActivity"> 
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>

Now, when we run the application, we will get output as shown below.

UK Academe-ToggleButton