Native Mobile App Development With Java

Android Switch

15-Sep-2024

Tutorial on how to implement the Switch widget in Android Studio Project Java

In Android, you can use the Switch widget to create an ON/OFF toggle button. Here's a basic example of how you can implement a Switch button in an Android application:


Here is the tutorial for Kotlin



There Are Some Common Attributes for TextView:


id
This is unique identity.
layout_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size
layout_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size
textSize
Change The Text Size
textColor
Change Text Color
textOn Set the text when the toggle button is in the ON / Checked
textOffSet the text when toggle button is in OFF / Unchecked
textColorChange the color of the text.
textSizeUsed to specify the size of the text
textStyleChange the style (bold, italic, normal) of text
background Background color Change
paddingset the padding from left, right, top and bottom


Step 1:

File open res/layout/activity_main.xml file −



<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_margin="20dp"
android:orientation="vertical">

<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="On"
android:checked="true"
android:textOff="Off"
android:text="Switch" />

<Switch
android:id="@+id/switch2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="On"
android:textOff="Off"
android:checked="true"
android:text="Switch" />

<Button
android:id="@+id/buttonSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />

</LinearLayout>
</RelativeLayout>


Step2 :

File open com.microappvalley.newtestproject/MainActivity.java file −:

Java:


package com.microappvalley.newtestproject;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

Switch switch1;
Switch switch2;
Button submitButton;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

switch1=findViewById(R.id.switch1);
switch2=findViewById(R.id.switch2);
submitButton=findViewById(R.id.buttonSubmit);
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (switch1.isChecked()){
Toast.makeText(MainActivity.this, "switch 1 On", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "switch 1 Off", Toast.LENGTH_SHORT).show();

}
}
});
switch2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (switch2.isChecked()){
Toast.makeText(MainActivity.this, "switch 2 On", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this, "switch 2 Off", Toast.LENGTH_SHORT).show();

}
}
});
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String switch1Data="";
String switch2Data="";
if (switch1.isChecked()){
switch1Data="Switch 1 On";
}else {
switch1Data="Switch 1 Off";
}
if (switch2.isChecked()){
switch2Data="Switch 2 On";
}else {
switch2Data="Switch 2 Off";
}
Toast.makeText(MainActivity.this, switch1Data+"\n"+switch2Data, Toast.LENGTH_SHORT).show();

}
});
}
}



Open Device Manager, run the emulator, and  then run the application. Next, check the working output and check the output  you declared in your code.


Output:


Comments