[spacer height=”20px”]

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.


[spacer height=”20px”]
[spacer height=”20px”]

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



[spacer height=”20px”]

Output :



Show Buttons
Hide Buttons
Read previous post:
Want to get round corner for you android button ?