Open In App

How to Add Custom Switch using IconSwitch Library in Android?

Last Updated : 30 Aug, 2022
Like Article

In this article, we will learn about how to add Custom Switch to our project. As we know the Switch has only two states ON and OFF. In Custom Switch, we add icons that can be used for different purposes depending upon our requirements. With the help of this library IconSwitch, we can easily add a custom switch and it animates automatically when the user changes the state.

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. The code for that has been given in both Java and Kotlin Programming Language for Android.

Step 2: Adding Dependency to the build.gradle File

 Go to Module build.gradle file and add this dependency and click on Sync Now button.

implementation 'com.polyak:icon-switch:1.0.0'

Step 3: Working with the XML Files

Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""
        app:isw_icon_size="25dp" />

Step 4: Working with the MainActivity File

Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail. Here we add setCheckChangedListener on our switch which will get invoked if the switch is changed. If the left switch is turned on then case LEFT is executed and if the right switch is turned on then case RIGHT is executed. 


import android.os.Bundle;
import android.widget.Toast;
import com.polyak.iconswitch.IconSwitch;
public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        IconSwitch iconSwitch = findViewById(;
        iconSwitch.setCheckedChangeListener(new IconSwitch.CheckedChangeListener() {
            public void onCheckChanged(IconSwitch.Checked current) {
                switch (current) {
                    case LEFT:
                        Toast.makeText(MainActivity.this, "Algorithms", Toast.LENGTH_SHORT).show();
                    case RIGHT:
                        Toast.makeText(MainActivity.this, "Courses", Toast.LENGTH_SHORT).show();


import android.os.Bundle
import android.widget.Toast
import com.polyak.iconswitch.IconSwitch
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        val iconSwitch = findViewById<IconSwitch>(
        iconSwitch.setCheckedChangeListener(object : CheckedChangeListener() {
            fun onCheckChanged(current: IconSwitch.Checked?) {
                when (current) {
                    LEFT -> Toast.makeText(this@MainActivity, "Algorithms", Toast.LENGTH_SHORT)
                    RIGHT -> Toast.makeText(this@MainActivity, "Courses", Toast.LENGTH_SHORT).show()


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads