Android Switch Button Example

Here you will get android switch button example.

Switch is a button which has two states, on or off. The user can on or off by just dragging or tapping finger on the button. This UI widget is supported on android version 4.0 and higher.

We can define switch button in XML layout by using <Switch> tag.

The OnCheckedChangeListener is applied on button to get notified whenever its state is changed.

The isChecked() method is used to check the current state of switch. It returns true if state is ON else returns false.

You can set the initial state of switch by setChecked() method in following way.

 

Android Switch Button Example

activity_main.xml

 

MainActivity.java

 

Screenshot

Android Switch Button Example

 

Comment below if you have any queries related to above android switch button example.

Leave a Reply

Your email address will not be published. Required fields are marked *