ToggleButton in Android
ToggleButton is used to allow users to perform two operations on a single button. These are used to perform on and off operations like that of Switch. ToggleButton can perform two different operations on clicking on it. In this article, we will take a look at How to implement ToggleButton in Android.
Note: This Android article covered in both Java and Kotlin languages.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Step 2: Working with the activity_main.xml file
Navigate to app > res > layout > activity_main.xml and add the below code to it. Comments are added in the code to get to know in detail.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:id = "@+id/idRLContainer" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" tools:context = ".MainActivity" > <!--on below line we are creating a text for our app--> < TextView android:id = "@+id/idTVHeading" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_above = "@id/idTVStatus" android:layout_centerInParent = "true" android:layout_margin = "20dp" android:gravity = "center" android:padding = "10dp" android:text = "Toggle Button in Android" android:textAlignment = "center" android:textColor = "@color/black" android:textSize = "20sp" android:textStyle = "bold" /> <!--on below line we are creating a text view--> < TextView android:id = "@+id/idTVStatus" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_above = "@id/idBtnToggle" android:layout_centerInParent = "true" android:layout_margin = "20dp" android:gravity = "center" android:padding = "10dp" android:text = "Status" android:textAlignment = "center" android:textColor = "@color/black" android:textSize = "20sp" android:textStyle = "bold" /> <!--on below line we are creating a toggle button--> < ToggleButton android:id = "@+id/idBtnToggle" android:layout_width = "match_parent" android:layout_height = "65dp" android:layout_centerInParent = "true" android:layout_margin = "20dp" android:padding = "10dp" android:textAllCaps = "true" android:textColor = "@color/black" android:textOff = "Off" android:textOn = "On" /> </ RelativeLayout > |
Step 3: Working with the MainActivity file
Navigate to app > java > your app’s package name > MainActivity file and add the code below. Comments are added in the code to get to know in detail.
Kotlin
package com.gtappdevelopers.kotlingfgproject import android.os.Bundle import android.widget.TextView import android.widget.ToggleButton import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { // on below line we are creating a variable. lateinit var toggleBtn: ToggleButton lateinit var statusTV: TextView override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // on below line we are initializing our variables. toggleBtn = findViewById(R.id.idBtnToggle) statusTV = findViewById(R.id.idTVStatus) // on below line we are setting text view // by checking toggle button status. if (toggleBtn.isChecked) { // if button is checked we are setting // text as Toggle Button is On statusTV.text = "Toggle Button is On" } else { // if button is unchecked we are setting // text as Toggle Button is Off statusTV.text = "Toggle Button is Off" } // on below line we are adding click listener // for our toggle button. toggleBtn.setOnClickListener { // on below line we are checking if // toggle button is checked or not. if (toggleBtn.isChecked) { // on below line we are setting message for // status text view if toggle button is checked. statusTV.text = "Toggle Button is On" } else { // on below line we are setting message // for status text view if toggle button is un checked. statusTV.text = "Toggle Button is Off" } } } } |
Java
package com.gtappdevelopers.kotlingfgproject; import android.os.Bundle; import android.view.View; import android.widget.TextView; import android.widget.ToggleButton; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { // on below line we are creating a variable. private ToggleButton toggleBtn; private TextView statusTV; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); // on below line we are initializing our variables. toggleBtn = findViewById(R.id.idBtnToggle); statusTV = findViewById(R.id.idTVStatus); // on below line we are setting text view by // checking toggle button status. if (toggleBtn.isChecked()) { // if button is checked we are setting // text as Toggle Button is On statusTV.setText( "Toggle Button is On" ); } else { // if button is unchecked we are setting // text as Toggle Button is Off statusTV.setText( "Toggle Button is Off" ); } // on below line we are adding click listener for our toggle button toggleBtn.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { // on below line we are checking if // toggle button is checked or not. if (toggleBtn.isChecked()) { // on below line we are setting message // for status text view if toggle button is checked. statusTV.setText( "Toggle Button is On" ); } else { // on below line we are setting message for // status text view if toggle button is un checked. statusTV.setText( "Toggle Button is Off" ); } } }); } } |
Now run your application to see the output of it.
Output:
Contact Us