Skip to content
Related Articles

Related Articles

Improve Article

How to add Custom Switch using IconSwitch Library in android ?

  • Last Updated : 15 Sep, 2021
Geek Week

In this article, we will learn about how to add Custom Switch in 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 depends 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.


  1. Add the support Library in build.gradle file and add dependency in the dependencies section. With the help of this we can directly use the widget in the layout.

    dependencies {         
          implementation 'com.polyak:icon-switch:1.0.0'      
  2. Add the following code in activity_main.xml file. Here we add the custom switch in our layout. In app:isw_icon_left and app:isw_icon_right, we add the icons from drawable for left and right switch respectively.


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

  3. Now add the following code in the file. Here we add setCheckChangedListener on our switch which will get invoked if the switch is changed. If left switch is turned on then case LEFT is executed and if right switch is turned on then case RIGHT is executed.

    package org.geeksforgeeks.gfgCustomSwitch;
    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(;
                new IconSwitch.CheckedChangeListener() {
                    public void onCheckChanged(IconSwitch.Checked current)
                        switch (current) {
                        case LEFT:
                                           "Algorithms", Toast.LENGTH_SHORT)
                        case RIGHT:
                                           "Courses", Toast.LENGTH_SHORT)


Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :