Dynamic Switch in Kotlin
Android Switch is also a two-state user interface element that is used to toggle between ON and OFF as a button. By touching the button we can drag it back and forth to make it either ON or OFF.
The Switch element is useful when only two states require for activity either choose ON or OFF. We can add a Switch to our application layout by using the Switch object. By default, the state for the android Switch is OFF state. We can also change the state of Switch to ON by setting the android:checked = “true” in our XML layout file.
In android, we can create Switch control in two ways either by using Switch in XML layout file or creating it in Kotlin file dynamically.
Step by Step Implementation
Step 1: Creating 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.
The code for that has been given in both Kotlin Programming Language for Android.
Step 2: Working with activity_main.xml file
In this file, we use the LinearLayout and can not add the switch widget manually because it will be created dynamically in Kotlin file.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main"
android:gravity="center"
android:background="@color/white"
android:orientation="vertical">
</LinearLayout>
Step 3: Creating switches programmatically in MainActivity.kt file
Here, we initialize and define two switches and add dynamically by calling on the linearLayout.
val linearLayout: LinearLayout = findViewById(R.id.main)
linearLayout.addView(switch1)
linearLayout.addView(switch2)
then, set OnClickListener on both the switches for functioning like toggle of button and Toast message like this.
switch1.setOnCheckedChangeListener { _, isChecked ->
val msg = if (isChecked) "Switch1:ON" else "Switch1:OFF"
Toast.makeText(this@MainActivity, msg, Toast.LENGTH_SHORT).show()
}
switch2.setOnCheckedChangeListener { _, isChecked ->
val msg = if (isChecked) "Switch2:ON" else "Switch2:OFF"
Toast.makeText(this@MainActivity, msg, Toast.LENGTH_SHORT).show()
}
MainActivity.kt:
package org.geeksforgeeks.demo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.Toast
import androidx.appcompat.widget.SwitchCompat
class MainActivity : AppCompatActivity()
{
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Creating switch1 and switch2 programmatically
val switch1 = SwitchCompat(this)
val layoutParams = LinearLayout.LayoutParams(ViewGroup.
LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
switch1.layoutParams = layoutParams
switch1.text = "Switch1"
val switch2 = SwitchCompat(this)
val layoutParams2 = LinearLayout.LayoutParams(ViewGroup.
LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
switch2.layoutParams = layoutParams2
switch2.text = "Switch2"
val linearLayout: LinearLayout = findViewById(R.id.main)
// Adding Switches to LinearLayout
linearLayout.addView(switch1)
linearLayout.addView(switch2)
switch1.setOnCheckedChangeListener { _, isChecked ->
val msg = if (isChecked) "Switch1:ON" else "Switch1:OFF"
Toast.makeText(this@MainActivity, msg,
Toast.LENGTH_SHORT).show()
}
switch2.setOnCheckedChangeListener { _, isChecked ->
val msg = if (isChecked) "Switch2:ON" else "Switch2:OFF"
Toast.makeText(this@MainActivity, msg,
Toast.LENGTH_SHORT).show()
}
}
}