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.
Approach:
- 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'}chevron_rightfilter_none - 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.
activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><com.polyak.iconswitch.IconSwitchandroid:id="@+id/icon_switch"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_centerVertical="true"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"/></RelativeLayout>chevron_rightfilter_none - Now add the following code in the MainActivity.java 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.
MainActivity.java
packageorg.geeksforgeeks.gfgCustomSwitch;importandroid.os.Bundle;importandroid.widget.Toast;importandroidx.appcompat.app.AppCompatActivity;importcom.polyak.iconswitch.IconSwitch;publicclassMainActivityextendsAppCompatActivity {@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity);IconSwitch iconSwitch = findViewById(R.id.icon_switch);iconSwitch.setCheckedChangeListener(newIconSwitch.CheckedChangeListener() {@OverridepublicvoidonCheckChanged(IconSwitch.Checked current){switch(current) {caseLEFT:Toast.makeText(MainActivity.this,"Algorithms", Toast.LENGTH_SHORT).show();break;caseRIGHT:Toast.makeText(MainActivity.this,"Courses", Toast.LENGTH_SHORT).show();break;}}});}}chevron_rightfilter_none
Output:
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to Add OpenCV library into Android Application using Android Studio?
- How to add Slidr Library in Android?
- Android | How to add Radio Buttons in an Android Application?
- Why You Should Switch to Kotlin from Java to Develop Android Apps?
- Google Launcher-Style Implementation of Switch Icon in Android
- How to create a Circular image view in Android without using any library?
- How to create popup message using Alerter Library in android
- How to Create a CircularImageView in Android using hdodenhof Library?
- Image loading and caching library for Android | Set 1
- Image loading and caching library for Android | Set 2
- Volley Library in Android
- How to install Android Applications in Mobile Phone without USB Cables using Android Studio
- How to implement Swipe Down to Refresh in Android using Android Studio
- How to add TextSwitcher with animation in Android using Java
- How to Add RangeSeekbar in Android Using Kotlin?
- How to add a custom styled Toast in Android using Kotlin
- Android | AdMob Banner Ads for Android Studio
- Android | Android Application File Structure
- Android | AdMob Interstitial Ads for Android Studio
- Android | How to Create/Start a New Project in Android Studio?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

