How to add Toggle Button in an Android Application - GeeksforGeeks

19 Feb, 2021
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”
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.xmlacticity_main.xmlacticity_main.xml<RelativeLayout    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"      android:padding="16dp"      android:background="@color/white"      tools:context=".MainActivity">        <ToggleButton          android:id="@+id/toggleButton"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_centerHorizontal="true"          android:layout_centerVertical="true"          android:onClick="onToggleClick"          />      <TextView          android: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>  
res -> Layout -> Activity_Main.xml (or) Main.xml
acticity_main.xml
<RelativeLayout    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"      android:padding="16dp"      android:background="@color/white"      tools:context=".MainActivity">        <ToggleButton          android:id="@+id/toggleButton"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_centerHorizontal="true"          android:layout_centerVertical="true"          android:onClick="onToggleClick"          />      <TextView          android: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>  
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.javaMainActivity.JavaMainActivity.Javaimport androidx.appcompat.app.AppCompatActivity;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;    @Override    protected void onCreate(        Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          togglebutton            = (ToggleButton)findViewById(                R.id.toggleButton);          textview            = (TextView)findViewById(                R.id.textView);    }      public void onToggleClick(View view)    {        if (togglebutton.isChecked()) {            textview.setText("Toggle is ON");        }        else {            textview.setText("Toggle is OFF");        }    }}
Open the app -> Java -> Package -> Mainactivity.java
MainActivity.Java
import androidx.appcompat.app.AppCompatActivity;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;    @Override    protected void onCreate(        Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);          togglebutton            = (ToggleButton)findViewById(                R.id.toggleButton);          textview            = (TextView)findViewById(                R.id.textView);    }      public void onToggleClick(View view)    {        if (togglebutton.isChecked()) {            textview.setText("Toggle is ON");        }        else {            textview.setText("Toggle is OFF");        }    }}
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.
Android-Button
Android
Java
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Create and Add Data to SQLite Database in Android?
Resource Raw Folder in Android Studio
Broadcast Receiver in Android With Example
Services in Android with Example
Android RecyclerView in Kotlin
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples