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

 

 

Then

 

 

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 :

toggle2

toggle1

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

Close