Prerequisite

If you not understand this lecture Toggle button in android studio.You should also read my previous lectures. Checkbox creation using android studio and Radio button using android studio. After reading these lecture you will also able to create Toggle and Switch button of your app.

Overview

Toggle button in android is very useful.Toggle button consists two states on or off.It is best alternative of Radio button.Toggle button provides facility to user to change state on or off.API 14 provides a other facility introduced a switch button.which is best alternative of Toggle button.

Introduction

In this lecture well we describe how can create Toggle and Switch button in android.We will also describe Toggle button and Switch button description.what is Toggle and Switch button . why we can use it .After Reading this lecture you will understand Toggle and Switch button in android And i hope you will create Toggle and Switch button  of your application.

Description of Toggle Button

In this lecture we will describe how to working with toggle button in android application. Toggle button in android is very useful . A toggle button takes only two states On state and Off state. This is the best alternative for radio buttons.We can easily create toggle buttons in android using <togglebutton> tag in android layout.Using toggle button you can change the state on or off. For example you can change state Wi-Fi signal on or off And you can also change state Bluetooth on or off using toggle button. So I will create application in android that will Demonstrate how can place a toggle button in android .And how to handle click events in toggle button. First of all you will create a new project in android studio.  Every project contain two file first is java class file and second is layout XML file your first step is open a layout XML file. and add toggle button and set height and width and assign id to  each toggle button and also assign text on or off state in layout XML. And add a text view that will show result .Now we add on click in a toggle button. when user click on button show it is on state or off state.For more detail about toggle button in android click on toggle button in android we also add a switch button switch button also perform a same functionality of toggle button.but switch button is available on API 14 to onward all android application.when you working with switch button your minimum SDK14 .Now you will open Gradel Scripts and change SDK version minimum 14.  show in below pic

Alt Tag android gradle script

XML Layout 

So we add a Switch and Toggle button into this layout and set height and width also assign text on or off state and add a text view that will show result.

Different property that are used in layout.xml file  explain here

  • warp_content   warp_content means height and width similar to the text that are entered.
  • Match_parent   means height and width similar to parent.
  • Toggle button     provides option between two elements off or no.
  • Switch button   which is best alternative of Toggle button also two state on off
  • Edit text              show massage  on state and off state

In this example we are going to create a simple UI that includes only (toggle button in android ) with Different attributes.And paste the following code toggle button in android into layout .XML file.

Now you may open the Graphical layout editor to preview the User Interface you created of toggle button  in android.

Alt Tag android switch and toggle button

Main Activity

Toggle and Switch button are sub classes of button so we implement as  a same way.So we use on click method when user click on button show a message it is on state or off state.It can be dividing into two alternative if and else…. When button state  is on then if statement is invoke. When button state  is off then else statement is invoke.

Code that are write in java class file of toggle button in android

Run the application of  button toggle in android

Alt Tag android switch and toggle button

When you change the state of Toggle button  ON then this message will show in below pic

Alt Tag andorid output toggle button image

When you change the state of switch button ON then this message will show in below pic

Alt Tag android switch output image

Thank you  for reading this lecture Hope you got the idea.