Android Switch Kotlin

15-Sep-2024

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

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 Java




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.kt file −:



package com.microappvalley.newtestproject

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Switch
import android.widget.Toast

class MainActivity : AppCompatActivity() {
@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val switch1:Switch=findViewById(R.id.switch1)
val switch2:Switch=findViewById(R.id.switch2)
val buttonSubmit:Button=findViewById(R.id.buttonSubmit)


switch1.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked){
Toast.makeText(this@MainActivity, "Switch1 Button ON", Toast.LENGTH_SHORT).show()
}else{
Toast.makeText(this@MainActivity, "Switch1 Button OFF", Toast.LENGTH_SHORT).show()
}
}

switch2.setOnCheckedChangeListener { buttonView, isChecked ->
if (isChecked){
Toast.makeText(this@MainActivity, "Switch2 Button ON", Toast.LENGTH_SHORT).show()
}else{
Toast.makeText(this@MainActivity, "Switch3 Button OFF", Toast.LENGTH_SHORT).show()
}
}

buttonSubmit.setOnClickListener {

var switch1String=""
var switch2String=""
if (switch1.isChecked){
switch1String="Switch 1 ON"
}else{
switch1String="Switch 1 OFF"
}

if (switch2.isChecked){
switch2String="Switch 2 ON"
}else{
switch2String="Switch 2 OFF"
}

Toast.makeText(this@MainActivity,switch1String+"\n"+switch2String,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