Custom ON-OFF Switch in kotlin

Activity.Main

package com.app.customswitch

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.app.customswitch.databinding.ActivityMainBinding
import com.app.customswitch.R

class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
var isOn = true
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.switchLayout.setOnClickListener {
toggleSwitch()
isOn = !isOn
}
}

fun toggleSwitch() {
    if (isOn) {
        binding.tvOn.setBackgroundResource(0)
        binding.tvOn.setTextColor(ContextCompat.getColor(this, R.color.white))
        binding.tvOff.setBackgroundResource(R.drawable.switch_on_bg)
        binding.tvOff.setTextColor(ContextCompat.getColor(this, R.color.orange))
    } else {
        binding.tvOff.setBackgroundResource(0)
        binding.tvOff.setTextColor(ContextCompat.getColor(this, R.color.white))
        binding.tvOn.setBackgroundResource(R.drawable.switch_on_bg)
        binding.tvOn.setTextColor(ContextCompat.getColor(this, R.color.orange))
    }
}

}

activitymain.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <LinearLayout
        android:id="@+id/switchLayout"
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        android:layout_margin="84dp"
        android:background="@drawable/main_layout_bg"
        android:orientation="horizontal"
        android:padding="3dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/tvOn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="5dp"
            android:text="ON"
            android:textColor="@color/white" />

        <TextView
            android:id="@+id/tvOff"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="OFF"
            android:background="@drawable/switch_on_bg"
            android:padding="5dp"
            android:textColor="@color/orange" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

1 thought on “Custom ON-OFF Switch in kotlin”

  1. Pingback: How to Add lottie animation in android studio using kotlin - codequest.blog

Leave a Comment

Your email address will not be published. Required fields are marked *