1. Introduction :


Android has provided wide variety’s of options for developers to make their application much more flexible for user.

Generally there will  be few options like vibration on or off , lights on or off and many other types of functionality where there is a requirement of On /Off we can easily use toggle button.

Not only On/Off it can be used according to our requirement in this tutorial i am showing it as On Off Button for turning light on and off.


2. Creating activity_main.xml

Add a toggle button to activity as


And also  a textview to show output






3. Creating MainActivity.java

Initialize Toggle Button


Then setOnClickListener for toggle button and read the state of Toggle Button and set it to TextView we have added to show output.



Full MainActivity.java Code



Output :



Share article on

Show Buttons
Hide Buttons

June 27, 2017

Attention Required! | Cloudflare

One more step

Please complete the security check to access push.wppush.co

Why do I have to complete a CAPTCHA?

Completing the CAPTCHA proves you are a human and gives you temporary access to the web property.

What can I do to prevent this in the future?

If you are on a personal connection, like at home, you can run an anti-virus scan on your device to make sure it is not infected with malware.

If you are at an office or shared network, you can ask the network administrator to run a scan across the network looking for misconfigured or infected devices.

Clear all
Read previous post:
Want to get round corner for you android button ?