Switch is a widget used in android applications for performing two-state operations such as on or off. The switch provides functionality where the user can change the settings between on and off using the switch. In this article, we will take a look at How to implement Switch in Android. A sample video is given below to get an idea about what we are going to do in this article.
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 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 = "Switch 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/idSwitch" 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 switch--> < Switch android:id = "@+id/idSwitch" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerInParent = "true" android:layout_margin = "10dp" /> </ 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.Switch import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { // on below line we are creating a variable. lateinit var switch : Switch 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. switch = findViewById(R.id.idSwitch) statusTV = findViewById(R.id.idTVStatus) // on below line we are // checking the status of switch if ( switch .isChecked) { // on below line we are setting text // if switch is checked. statusTV.text = "Switch is Checked" } else { // on below line we are setting the // text if switch is un checked statusTV.text = "Switch is UnChecked" } // on below line we are adding check change listener for our switch. switch .setOnCheckedChangeListener { buttonView, isChecked -> // on below line we are checking // if switch is checked or not. if (isChecked) { // on below line we are setting text // if switch is checked. statusTV.text = "Switch is Checked" } else { // on below line we are setting text // if switch is unchecked. statusTV.text = "Switch is UnChecked" } } } } |
Java
package com.gtappdevelopers.kotlingfgproject; import android.os.Bundle; import android.widget.CompoundButton; import android.widget.Switch; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { // on the below line we are creating a variable. private Switch switchView; 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. switchView = findViewById(R.id.idSwitch); statusTV = findViewById(R.id.idTVStatus); // on below line we are checking // the status of switch if (switchView.isChecked()) { // on below line we are setting text // if switch is checked. statusTV.setText( "Switch is Checked" ); } else { // on below line we are setting the text // if switch is un checked statusTV.setText( "Switch is UnChecked" ); } // on below line we are adding check change listener for our switch. switchView.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // on below line we are checking // if switch is checked or not. if (isChecked) { // on below line we are setting text // if switch is checked. statusTV.setText( "Switch is Checked" ); } else { // on below line we are setting text // if switch is unchecked. statusTV.setText( "Switch is UnChecked" ); } } }); } } |
Now run your application to see the output of it.
Output: