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.
Approach
-
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
acticity_main.xml
<RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:padding="16dp"android:background="@color/white"tools:context=".MainActivity"><ToggleButtonandroid:id="@+id/toggleButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_centerVertical="true"android:onClick="onToggleClick"/><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="100dp"android:layout_centerVertical="true"android:layout_centerHorizontal="true"android:layout_above="@+id/toggleButton"android:textStyle="bold"android:textColor="@color/black"/></RelativeLayout>chevron_rightfilter_none -
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 -> Mainactivity.java
MainActivity.Java
importandroidx.appcompat.app.AppCompatActivity;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.TextView;importandroid.widget.ToggleButton;publicclassMainActivityextendsAppCompatActivity {ToggleButton togglebutton;TextView textview;@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);togglebutton= (ToggleButton)findViewById(R.id.toggleButton);textview= (TextView)findViewById(R.id.textView);}publicvoidonToggleClick(View view){if(togglebutton.isChecked()) {textview.setText("Toggle is ON");}else{textview.setText("Toggle is OFF");}}}chevron_rightfilter_none
Output:
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.



Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Android | How to add Radio Buttons in an Android Application?
- Android | Android Application File Structure
- Android | How to Request permissions in Android Application?
- How to add a Pie Chart into an Android Application
- How to Add Widget of an Android Application?
- Components of an Android Application
- The Application Manifest File | Android
- How to change Input Method Action Button in Android?
- Android | AdMob Banner Ads for Android Studio
- Android | AdMob Interstitial Ads for Android Studio
- Android | How to Create/Start a New Project in Android Studio?
- Android | Running your first Android app
- Difference between Android 1.1 and Android 4.0.1
- Difference between Android 1.1 and Android 2.3.4
- Difference between Android 1.0 and Android 1.1
- Difference between Android 1.0 and Android 7.1
- Difference between Android 1.0 and Android 2.3
- Difference between Android 1.0 and Android 2.2
- Difference between Android 1.0 and Android 1.6
- Difference between Android 1.0 and Android 2.1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

