ToggleButton is an Android widget used to change between two states.Here in this tutorial, we are going to glow bulb using ToggleButton.We will set ImageView to an image with a bulb on the image and toggle it alternatively.

Toggle Button

Toggle Button

Step 1: Drag and Drop ImageView and Toggle button from palette or code it in the XML with the code below.

Step 2: Create an object of ImageView and ToggleButton and typecast it ImageView and Toggle with their id as shown below.

Step 3: Use object of ToggleButton and setOnCheckedChange Listener and use if-else statement to set ImageView to images with bulb-on and off images.


Note: Use two bulb image with on and off states and drop it in the drawable folder.

Decode The Code: We have created the object of ToggleButton and ImageView then we called onCheckedChanged listener to set ImageView to two Bulb images which toggles between using if-else tatements.