Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
The IconSwitch
library provides a custom switch for Android which resembles the iOS segmented control. The switch allows the user to select between two options, typically referred to as "checked" and "unchecked" or "on" and "off".
Here's how you can integrate the IconSwitch
library and use it in your Android application:
First, you need to add the library to your build.gradle
(Module: app):
implementation 'com.polyak:icon-switch:1.1.3'
After syncing your project with the Gradle files, you can add the IconSwitch
to your XML layout:
<com.polyak.iconswitch.IconSwitch android:id="@+id/icon_switch" android:layout_width="wrap_content" android:layout_height="wrap_content" app:is_checkedIcon="@drawable/your_checked_icon" app:is_uncheckedIcon="@drawable/your_unchecked_icon" app:is_textChecked="On" app:is_textUnchecked="Off" app:is_checkedColor="@color/colorPrimary" app:is_uncheckedColor="@color/colorSecondary"/>
Replace your_checked_icon
and your_unchecked_icon
with the drawables you'd like to use for each state. You can also customize other properties, such as text and colors, using the provided attributes.
In your activity or fragment, you can set a listener to react to changes in the switch state:
val iconSwitch: IconSwitch = findViewById(R.id.icon_switch) iconSwitch.setCheckedChangeListener { isChecked -> if (isChecked) { // Handle checked state } else { // Handle unchecked state } }
To get the current state of the switch:
val currentState = iconSwitch.checked
To programmatically set the state:
iconSwitch.checked = true // for checked iconSwitch.checked = false // for unchecked
With these steps, you'll be able to use IconSwitch
in your Android app to provide an interactive switch with customizable icons and text.
Implementing custom switch with IconSwitch in Android:
<!-- Add the IconSwitch library dependency in your build.gradle file --> implementation 'com.github.KeepSafe:IconSwitch:1.0.3' <!-- Include IconSwitch in your layout file --> <com.github.KeepSafe.IconSwitch.IconSwitch android:id="@+id/iconSwitch" android:layout_width="wrap_content" android:layout_height="wrap_content" app:isw_checked="true" app:isw_icon="@drawable/ic_custom_icon" app:isw_icon_margin="8dp" />
Customizing appearance and icons in IconSwitch for Android:
<com.github.KeepSafe.IconSwitch.IconSwitch android:id="@+id/iconSwitch" android:layout_width="wrap_content" android:layout_height="wrap_content" app:isw_checked="true" app:isw_icon="@drawable/ic_custom_icon" app:isw_icon_margin="8dp" app:isw_icon_off="@drawable/ic_custom_icon_off" app:isw_icon_on="@drawable/ic_custom_icon_on" app:isw_background_color="@color/custom_background_color" />
Handling state changes and events in IconSwitch:
val iconSwitch = findViewById<IconSwitch>(R.id.iconSwitch) iconSwitch.setCheckedChangeListener(object : IconSwitch.CheckedChangeListener { override fun onCheckChanged(isChecked: Boolean) { // Handle switch state change } })
Adding labels or text to a custom switch using IconSwitch:
<com.github.KeepSafe.IconSwitch.IconSwitch android:id="@+id/iconSwitch" android:layout_width="wrap_content" android:layout_height="wrap_content" app:isw_checked="true" app:isw_icon="@drawable/ic_custom_icon" app:isw_icon_margin="8dp" app:isw_text="Custom Switch" />