Android Switch


Download Android Switch sample

Andriod Switch is a widget, which can have 2 state, one can select one of these 2 toggle states. You can drag the "thumb" back and forth to choose the selected option, or you can tap to toggle as if it were a checkbox.

Android Switch Usage?


Android Switch simple usage in xml layout is given below:

Android switch usage

Text styling in android Switch?


Android provides several text decorating attributes like Typeface, font family, text style, text size, text color, text shadow etc. which you can learn here:

How to decorate text in android

Xml attributes of Android Switch?


Let's discuss xml attributes of Android Switch

android:text - Showing caption text in Android Switch?

android:text can be used to show caption text in Android Switch

android:checked - How to set Android Switch state in xml?

To check an android switch in xml set android:checked attribute of Android Switch as true. To uncheck an Android Switch in xml set android:checked attribute of Android switch as false.

android:showText - Show/Hide text on Android switch when Switch is checked or unchecked"

android:showText specify whether android:textOff and android:textOn text will be shown or not.

android:showText="true" enforces system to draw android:textOff & android:textOn text. If we set android:showText="false" then android:textOff & android:textOn will not be shown.

android:textOff - Drawing text on Android switch when Switch is unchecked/"Off"

android:textOff specify the text to use when the switch is in the unchecked or "off" state.

Android switch textOff

android:textOn - Drawing text on Android switch when Switch is checked/"on"

android:textOn specify the text to use when the switch is in the checked or "on" state.

Android switch textOn

android:splitTrack - How to split the track and leave a gap for thumb drawable

android:splitTrack attribute of android Switch used to split the track and leave a gap for thumb drawable.

android:splitTrack must have a boolean value true or false

android:switchPadding - How to add space between caption text and Android Switch

android:switchPadding specifies the minimum space between the switch and caption text

android:thumb - How to change thumb icon of Android Switch

Using android:thumb attribute of Android Switch we can change the thumb icon of Android Switch

Android switch change thumb icon

android:thumbTextPadding - How to assign thumb text padding in Android Switch

android:thumbTextPadding attribute of Android Switch Amount of padding on either side of text within the switch thumb.

android:track - How to change track of Android Switch

android:thumbTextPadding attribute of Android Switch Amount of padding on either side of text within the switch thumb.

Android switch Custom track and custom thumb

How to handle click event of Android Switch

Android switch is a view and we can handle click event of Android Switch like any other Android View.

A simple way to handle click event of Android Switch is declare onClick attribute of Android Switch and implement that method in your activity class as given below:

isChecked method of Switch can be used to check whether Switch status is checked or not.

How to handle OnCheckedChangeListener of Android Switch?

OnClick event of Android Switch fires once user click on ANdroid Switch. What if we want to perform some action onCheckedChange event of Android Switch?

Android switch inherits CompoundButton's attributes, so implement CompoundButton.OnCheckedChangeListener interface and override onCheckedChanged method as follows:

Next step is to set setOnCheckedChangeListener of Android Switch button in onCreate method of activity as follows:

Android Switch demo

Android switch demo

In this example I have discussed different aspect of Android Switch. I have implemented onCheckedChangeListener as well as onClick event of Android Switch to demonstrate the difference.

Step 1: Create a new project in Android Studio and name it Android Switch.

Step 2: Open res -> layout -> activity_main.xml and add following code

activity_main.xml

Step 3: Open your MainActivity.java and replace its source with source of my MainActivity.java as follows:

MainActivity.java

Step 4: Clean and run the project to see the result.


Share & Like

Tutorial topics

About ApnaTutorials.com

ApnaTutorials.com is for Java, J2ee, Html5 and android developers. All tutorials are well explained and are easy to understand. We have tested every tutorials on our developement environment before posting it in public domain.