Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java
  • Android
  • Kotlin
  • Flutter
  • Dart
  • Android with Java
  • Android Studio
  • Android Projects
  • Android Interview Questions
Open In App
Next Article:
How to Add Custom Switch using IconSwitch Library in Android?
Next article icon

How to Add Custom Switch using IconSwitch Library in Android?

Last Updated : 10 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Note: This library is now deprecated and will not work in newer versions of Android Studio.

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.kts File

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

implementation ("com.polyak:icon-switch:1.0.0")


Step 3: Working with the Main XML Layout

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.

activity_main.xml:

XML
<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"     android:background="@color/white"     android:orientation="vertical"     tools:context=".MainActivity">      <com.polyak.iconswitch.IconSwitch         android:id="@+id/icon_switch"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         app:isw_default_selection="right"         app:isw_icon_left="@drawable/ic_algorithm"         app:isw_icon_right="@drawable/ic_online_course"         app:isw_icon_size="25dp"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toTopOf="parent"/>  </androidx.constraintlayout.widget.ConstraintLayout> 

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. 

MainActivity.java
package org.geeksforgeeks.demo;  import android.os.Bundle; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import com.polyak.iconswitch.IconSwitch;  public class MainActivity extends AppCompatActivity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity);          IconSwitch iconSwitch = findViewById(R.id.icon_switch);         iconSwitch.setCheckedChangeListener(new IconSwitch.CheckedChangeListener() {             @Override             public void onCheckChanged(IconSwitch.Checked current) {                 switch (current) {                     case LEFT:                         Toast.makeText(MainActivity.this, "Algorithms", Toast.LENGTH_SHORT).show();                         break;                     case RIGHT:                         Toast.makeText(MainActivity.this, "Courses", Toast.LENGTH_SHORT).show();                         break;                 }             }         });     } } 
MainActivity.kt
package org.geeksforgeeks.demo  import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.polyak.iconswitch.IconSwitch  class MainActivity : AppCompatActivity() {          override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)                  val iconSwitch = findViewById<IconSwitch>(R.id.icon_switch)         iconSwitch.setCheckedChangeListener(object : CheckedChangeListener() {             fun onCheckChanged(current: IconSwitch.Checked?) {                 when (current) {                     LEFT -> Toast.makeText(this@MainActivity, "Algorithms", Toast.LENGTH_SHORT)                         .show()                     RIGHT -> Toast.makeText(this@MainActivity, "Courses", Toast.LENGTH_SHORT).show()                 }             }         })     } } 

Output:


Next Article
How to Add Custom Switch using IconSwitch Library in Android?

M

madhavmaheshwarimm20
Improve
Article Tags :
  • Java
  • Kotlin
  • Android
  • DSA
  • Kotlin Android
  • Java-Android
Practice Tags :
  • Java

Similar Reads

    How to add Custom Spinner in Android?
    Spinner is a widget that is used to select an item from a list of items. When the user tap on a spinner a drop-down menu is visible to the user. In this article, we will learn how to add custom spinner in the app. Steps of Implementing Custom SpinnerStep 1: Create a new layout for each item in Spinn
    5 min read
    How to add TextSwitcher with animation in Android using Java
    A TextSwitcher is used to animate a text on a screen. It is the child class of the ViewSwitcher class. It contains only one child of type TextView. In order to set animation on TextSwitcher we have to add animation tag or we can add programmatically. Here are the some usage of TextSwitcher: Changing
    2 min read
    How to Add Share Button in Toolbar in Android?
    In this article, we are going to create a simple Share Button in the Toolbar in Android. The Share Button is used to share information on mail, Bluetooth, Facebook, Twitter, WhatsApp, etc to an individual person or a group on any social media. We can share any type of message like text, images, vide
    4 min read
    Switch Button in Android using Jetpack Compose
    A Switch or a Switch Button in Android is a UI element that is used to switch between two states upon click. It can be assumed as a Boolean button with two different values. Some states where you may find a Switch in your Android device can be WIFI ON and OFF, Bluetooth ON and OFF, Dark Mode and Lig
    2 min read
    How to Save Switch Button State in Android?
    In Android, a Switch is a type of button that lets the user toggle between two actions or instances. In general, a Switch is used for selecting one between two options that can be invoking any actions or functions. In this article, we are going to see how we can save the state of the Switch Button i
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences