{"id":859,"date":"2016-02-12T07:09:11","date_gmt":"2016-02-12T07:09:11","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=859"},"modified":"2019-06-12T11:36:11","modified_gmt":"2019-06-12T11:36:11","slug":"togglebutton","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/togglebutton","title":{"rendered":"ToggleButton (On\/Off) Tutorial With Example In Android"},"content":{"rendered":"<p>In Android, ToggleButton is used to display checked and unchecked state of a button. ToggleButton basically an off\/on button with a light indicator which\u00a0indicate the current state of toggle button. The most simple example of ToggleButton is doing\u00a0on\/off in sound, Bluetooth, wifi, hotspot etc.\u00a0It\u00a0is a subclass of compoundButton.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-956\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/ToggleButton-300x102.jpg\" alt=\"ToggleButton\" width=\"300\" height=\"102\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/ToggleButton-300x102.jpg 300w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/ToggleButton.jpg 492w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/center><span style=\"color: #008000;\"><strong>ToggleButton Vs Switch In Android:<\/strong><\/span><\/p>\n<p>ToggleButton allow the users to change the setting between two states like turn on\/off your wifi, Bluetooth etc from your phone\u2019s setting menu. Since, Android 4.0 version ( API level 14 ) there is an another kind of ToggleButton called Switch which\u00a0provide the user slider control. You can learn more about it reading <a href=\"\/ui\/switch\">Switch tutorial<\/a>.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-957\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/Switch-Vs-ToggleButton-in-Android-300x185.jpg\" alt=\"Switch Vs ToggleButton in Android\" width=\"300\" height=\"185\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Switch-Vs-ToggleButton-in-Android-300x185.jpg 300w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Switch-Vs-ToggleButton-in-Android.jpg 318w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/center><span style=\"color: #ff0000;\"><strong>Important\u00a0Note: <\/strong><\/span>Android Switch and ToggleButton both are the subclasses of CompoundButton class.<\/p>\n<p><strong>ToggleButton code in XML:<\/strong><\/p>\n<pre>&lt;ToggleButton\r\nandroid:id=\"@+id\/simpleToggleButton\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\/&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>How To Check Current State Of ToggleButton:<\/strong><\/span><\/p>\n<p>To check\u00a0current state of a toggle button programmatically we use\u00a0isChecked() method. This method returns a Boolean value either\u00a0true or false. If a toggle button is checked then it returns true otherwise it returns false. Below is\u00a0the\u00a0code which checks the current state of a toggle button.<\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\nToggleButton simpleToggleButton = (ToggleButton) findViewById(R.id.simpleToggleButton); \/\/ initiate a toggle button\r\nBoolean ToggleButtonState = simpleToggleButton.isChecked(); \/\/ check current state of a toggle button (true or false).<\/pre>\n<hr \/>\n<h4><strong>Attributes of ToggleButton:<\/strong><\/h4>\n<p>Now let\u2019s \u00a0we discuss important\u00a0attributes that helps us to configure a Toggle Button in XML\u00a0file (layout).<\/p>\n<p><span style=\"color: #008000;\"><strong>1. id:<\/strong><\/span>\u00a0id is an attribute used to uniquely identify a toggle button.<\/p>\n<pre>&lt;ToggleButton\r\n<strong>android:id=\"@+id\/simpleToggleButton\"<\/strong>\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\/&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. checked: <\/strong><\/span>checked is an attribute of toggle button used to set the current state of a toggle button. The value should be true or false where true shows the checked state and false shows unchecked state of a toggle button. The default value of checked attribute is false. We can also set the current state programmatically.<\/p>\n<p>Below we set true value for checked attribute sets the current state to checked.<\/p>\n<pre>&lt;ToggleButton\r\n    android:id=\"@+id\/simpleToggleButton\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:checked=\"true\" \/&gt;&lt;!-- set the current state of the toggle button--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-938\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/Checked-in-ToggleButton-300x240.jpg\" alt=\"Checked in ToggleButton\" width=\"300\" height=\"240\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Checked-in-ToggleButton-300x240.jpg 300w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Checked-in-ToggleButton.jpg 317w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/center><strong>Setting Checked Of ToogleButton Current State In Java Class:<\/strong><\/p>\n<p>Below we set the current state of toggle button to checked:<\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\nToggleButton simpleToggleButton = (ToggleButton) findViewById(R.id.<strong><em>simpleToggleButton<\/em><\/strong>); \/\/ initiate a toggle button\r\nsimpleToggleButton.setChecked(<strong>true<\/strong>); \/\/ set the current state of a toggle button\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>3. gravity: <\/strong><\/span>The gravity attribute is an optional attribute which is used to control the alignment of the text in ToogleButton like left, right, center, top, bottom, center_vertical, center_horizontal etc.<\/p>\n<pre>&lt;ToggleButton\r\n    android:id=\"@+id\/simpleToggleButton\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:layout_centerHorizontal=\"true\"\r\n    android:checked=\"true\"\r\n    android:gravity=\"right|center_vertical\"\/&gt;&lt;!-- gravity of the toggle button--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-942\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/gravity-in-ToggleButton-300x211.jpg\" alt=\"gravity in ToggleButton\" width=\"300\" height=\"211\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/gravity-in-ToggleButton-300x211.jpg 300w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/gravity-in-ToggleButton.jpg 318w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/center><span style=\"color: #008000;\"><strong>4. textOn And textOff:<\/strong><\/span> textOn attribute is used to set the text when toggle button is in checked\/on state. We can set the textOn in XML\u00a0as well as in the java class.<\/p>\n<p>Below is the example code with explanation included in which we set the textOn \u00a0\u201cEnabble\u00a0Attribute Of Toggle button\u201d for a toggle button.<\/p>\n<pre>&lt;ToggleButton\r\n    android:id=\"@+id\/simpleToggleButton\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:checked=\"true\"\r\n    android:layout_centerHorizontal=\"true\"\r\n    android:textOff=\"Disable\"\r\n    android:textOn=\"Enable\"\/&gt; &lt;!--text to be displayed whenever toggle button is checked--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-943 size-full\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/texton-and-textoff-in-ToogleButton-Android.jpg\" alt=\"texton and textoff in ToogleButton Android\" width=\"597\" height=\"184\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/texton-and-textoff-in-ToogleButton-Android.jpg 597w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/texton-and-textoff-in-ToogleButton-Android-300x92.jpg 300w\" sizes=\"auto, (max-width: 597px) 100vw, 597px\" \/><\/center><strong>Setting textOn and textOff Of ToggleButton In Java class:<\/strong><\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\n\/\/ initiate toggle button\r\nToggleButton simpleToggleButton = (ToggleButton) findViewById(R.id.simpleToggleButton);\r\n\/\/ displayed text of the toggle button whenever it is in checked or on state\r\nsimpleToggleButton.setTextOn(\"TextOn Attribute Of Toggle b3`utton\");\r\n\/\/ displayed text of the toggle button whenever it is in unchecked or off state\r\nsimpleToggleButton.setTextOff(\"TextOff Attribute Of Toggle b3`utton\");<\/pre>\n<p><span style=\"color: #008000;\"><strong>5. textColor:<\/strong><\/span> textColor attribute is used to set the text color of a toggle button. Color value is in the form of &#8220;#argb&#8221;, &#8220;#rgb&#8221;, &#8220;#rrggbb&#8221;, or &#8220;#aarrggbb&#8221;.<\/p>\n<p>Below we set the red color for the displayed text of a Toggle button.<\/p>\n<pre>&lt;ToggleButton\r\n    android:id=\"@+id\/simpleToggleButton\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:checked=\"false\"\r\n    android:textOff=\"On State\"\r\n    android:textOn=\"Off State\"\r\n    android:layout_centerHorizontal=\"true\"\r\n    android:textColor=\"#f00\" \/&gt;&lt;!--red color for displayed text--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-944\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/textColor-in-ToggleButton-Android.jpg\" alt=\"textColor in ToggleButton Android\" width=\"256\" height=\"190\" \/><\/center><strong>Setting textColor Of ToggleButton In Java class:<\/strong><\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\nToggleButton simpleToggleButton = (ToggleButton) findViewById(R.id.<strong><em>simpleToggleButton<\/em><\/strong>);\/\/ initiate toggle button\r\nsimpleToggleButton.setTextColor(Color.<strong><em>RED<\/em><\/strong>); \/\/red color for displayed text of toggle button\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>6. textSize:<\/strong><\/span>\u00a0textSize attribute set the size of the text of a toggle button. We can set the text size in sp(scale independent pixel) or dp(density pixel).<\/p>\n<p>Below we set the 25sp size for the text of a toggle button.<\/p>\n<pre>&lt;ToggleButton\r\n    android:id=\"@+id\/simpleToggleButton\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:checked=\"false\"\r\n    android:textOff=\"On State\"\r\n    android:textOn=\"Off State\"\r\n    android:layout_centerHorizontal=\"true\"\r\n    android:textColor=\"#f00\"\r\n    android:textSize=\"25sp\"\/&gt; &lt;!-- 25sp displayed text size--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-945\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/textSize-in-ToggleButton-Text-Android.jpg\" alt=\"textSize in ToggleButton Text Android\" width=\"255\" height=\"204\" \/><\/center><strong>Setting textSize Of ToggleButton Text In Java class:<\/strong><\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\nToggleButton simpleToggleButton = (ToggleButton) findViewById(R.id.<strong><em>simpleToggleButton<\/em><\/strong>); \/\/ initiate toggle button\r\nsimpleToggleButton.setTextSize(25); \/\/ set 25sp displayed text size of toggle button\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>7. textStyle:<\/strong><\/span> textStyle attribute is used to set the text style of the text of a Toggle button. You can set\u00a0bold, italic and normal.\u00a0 If you need to use two or more styles for a text view then \u201c|\u201d operator is used for that.<\/p>\n<p>Below we set the bold and italic text styles for text of a toggle button.<\/p>\n<pre>&lt;ToggleButton\r\n    android:id=\"@+id\/simpleToggleButton\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:checked=\"true\"\r\n    android:textOff=\"Off State\"\r\n    android:textOn=\"On State\"\r\n    android:textSize=\"25sp\"\r\n    android:layout_centerHorizontal=\"true\"\r\n    android:textColor=\"#f00\"\r\n    android:textStyle=\"bold|italic\"\/&gt; &lt;!-- bold and italic text style for displayed text--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-946\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/textStyle-in-ToggleButton-Android.jpg\" alt=\"textStyle in ToggleButton Android\" width=\"256\" height=\"216\" \/><\/center><span style=\"color: #008000;\"><strong>8. background:<\/strong><\/span> background attribute is used to set the background of a toggle button. We can set a color or a drawable in the background of a toggle button.<\/p>\n<p>Below we set the black color for the background and red color for the displayed text of a ToggleButton.<\/p>\n<pre>&lt;ToggleButton\r\n    android:id=\"@+id\/simpleToggleButton\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:checked=\"true\"\r\n    android:textOff=\"Off State\"\r\n    android:textOn=\"On State\"\r\n    android:textSize=\"25sp\"\r\n    android:layout_centerHorizontal=\"true\"\r\n    android:textStyle=\"bold|italic\"\r\n    android:textColor=\"#f00\"\r\n    android:background=\"#000\"\/&gt;&lt;!--Black Color Background--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-947\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/background-in-ToggleButton-Android.jpg\" alt=\"background in ToggleButton Android\" width=\"261\" height=\"208\" \/><\/center><strong>Setting Background\u00a0Of ToggleButton Text In Java class:<\/strong><\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\nToggleButton simpleToggleButton = (ToggleButton) findViewById(R.id.<strong><em>simpleToggleButton<\/em><\/strong>);\r\nsimpleToggleButton.setBackgroundColor(Color.<strong><em>BLACK<\/em><\/strong>);\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>9. padding:<\/strong><\/span>\u00a0padding attribute is used to set the padding from left, right, top or bottom.<\/p>\n<ul>\n<li><strong>paddingRight :<\/strong>set the padding from the right side of the toggle button<strong>.<\/strong><\/li>\n<li><strong>paddingLeft :<\/strong>set the padding from the left side of the toggle button<strong>.<\/strong><\/li>\n<li><strong>paddingTop :<\/strong>set the padding from the top side of the toggle button<strong>.<\/strong><\/li>\n<li><strong>paddingBottom :<\/strong>set the padding from the bottom side of the toggle button<strong>.<\/strong><\/li>\n<li><strong>Padding :<\/strong>set the padding from the all side\u2019s of the toggle button<strong>.<\/strong><\/li>\n<\/ul>\n<p>Below we set the 40dp padding from all the side\u2019s of the toggle button.<\/p>\n<pre>&lt;ToggleButton\r\n    android:id=\"@+id\/simpleToggleButton\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:checked=\"true\"\r\n    android:textOff=\"Off State\"\r\n    android:textOn=\"On State\"\r\n    android:textSize=\"25sp\"\r\n    android:layout_centerHorizontal=\"true\"\r\n    android:textColor=\"#f00\"\r\n    android:padding=\"40dp\"\/&gt; &lt;!--40dp padding from all the side's of toggle button--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-948\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/padding-in-ToggleButton.jpg\" alt=\"padding in ToggleButton\" width=\"253\" height=\"243\" \/><\/center><span style=\"color: #008000;\"><strong>10. drawableBottom, drawableTop, drawableRight And drawableLeft: <\/strong><\/span>These attribute draw the drawable below, top, right and left of the text of ToggleButton.<\/p>\n<p>Below we set the icon to the Top\u00a0of the text of a ToggleButton. In the similar way you can try for other three attribute yourself.<\/p>\n<pre>&lt;!--Make sure to add ic_launcher image in drawable folder--&gt;\r\n&lt;ToggleButton\r\n    android:id=\"@+id\/simpleToggleButton\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:checked=\"true\"\r\n    android:textOff=\"Off State\"\r\n    android:textOn=\"On State\"\r\n    android:layout_centerHorizontal=\"true\"\r\n    android:textColor=\"#000\"\r\n    android:drawableTop=\"@drawable\/ic_launcher\" \/&gt;&lt;!--drawable icon at the bottom of text of top buttton--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-949\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/drawableTop-in-ToggleButton-Android.jpg\" alt=\"drawableTop in ToggleButton Android\" width=\"256\" height=\"212\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>ToggleButton Example In Android Studio:<\/strong><\/h4>\n<p>Below is the example of ToggleButton in Android Studio. In this example we display two toggle button with background and one \u201csubmit\u201d button using attributes discussed earlier in this post.\u00a0Whenever user\u00a0click on the submit button, the current state of\u00a0both toggle button\u2019s is displayed in a Toast. Below is the final output, download code and step by step explanation:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/ToggleButtonExample\" target=\"_blank\" rel=\"nofollow\">Download Code<\/a><a class=\"help\" title=\"Learn How To Download Code And Import In Android Studio\" href=\"\/androidstudio\/download-code-abhiandroid\" target=\"_blank\" rel=\"nofollow\"> ? <\/a><\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-954\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/ToggleButton-Example-in-Android-Studio-183x300.jpg\" alt=\"ToggleButton Example in Android Studio\" width=\"183\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/ToggleButton-Example-in-Android-Studio-183x300.jpg 183w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/ToggleButton-Example-in-Android-Studio.jpg 199w\" sizes=\"auto, (max-width: 183px) 100vw, 183px\" \/><\/center><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project and name it ToggleButtonExample<\/p>\n<p>In this step we create a new project for ToggleButton in Android Studio by filling all the necessary\u00a0details of the app like app name, package name, api versions etc.<\/p>\n<pre>Select File -&gt; New -&gt; New Project -&gt; Fill the forms and click \"Finish\" button.<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Open res -&gt; layout -&gt;\u00a0activity_main.xml (or) main.xml\u00a0and add following code:<\/p>\n<p>In this step we open an xml file and add the code for displaying two toggle button and one normal Button.<\/p>\n<pre>&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:orientation=\"vertical\"\r\n    android:paddingBottom=\"@dimen\/activity_vertical_margin\"\r\n    android:paddingLeft=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingRight=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingTop=\"@dimen\/activity_vertical_margin\"\r\n    tools:context=\".MainActivity\"&gt;\r\n\r\n    &lt;LinearLayout\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_gravity=\"center_horizontal\"\r\n        android:orientation=\"horizontal\"&gt;\r\n\r\n        &lt;ToggleButton\r\n            android:id=\"@+id\/simpleToggleButton1\"\r\n            android:layout_width=\"wrap_content\"\r\n            android:layout_height=\"wrap_content\"\r\n            android:layout_gravity=\"center_horizontal\"\r\n            android:checked=\"false\"\r\n            android:drawablePadding=\"20dp\"\r\n            android:drawableRight=\"@drawable\/ic_launcher\"\r\n            android:textColor=\"#000\" \/&gt;\r\n\r\n        &lt;ToggleButton\r\n            android:id=\"@+id\/simpleToggleButton2\"\r\n            android:layout_width=\"wrap_content\"\r\n            android:layout_height=\"wrap_content\"\r\n            android:layout_gravity=\"center_horizontal\"\r\n            android:layout_marginLeft=\"50dp\"\r\n            android:checked=\"true\"\r\n            android:drawableLeft=\"@drawable\/ic_launcher\"\r\n            android:drawablePadding=\"20dp\"\r\n            android:textColor=\"#000\" \/&gt;\r\n    &lt;\/LinearLayout&gt;\r\n\r\n    &lt;Button\r\n        android:id=\"@+id\/submitButton\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_gravity=\"center\"\r\n        android:layout_marginTop=\"50dp\"\r\n        android:background=\"#0f0\"\r\n        android:padding=\"10dp\"\r\n        android:text=\"Submit\"\r\n        android:textColor=\"#fff\"\r\n        android:textSize=\"20sp\"\r\n        android:textStyle=\"bold\" \/&gt;\r\n&lt;\/LinearLayout&gt;<\/pre>\n<p>Step 3: Open \u00a0 app\u00a0-&gt; java -&gt; package -&gt;\u00a0<strong>MainActivity.<\/strong><strong>java<\/strong><\/p>\n<p>In this step we open\u00a0MainActivity\u00a0where\u00a0we add the code to\u00a0initiate the Toggle Buttons and normal Button. After initiating we\u00a0perform click event on button and display the text of current state of ToggleButton using a Toast.<\/p>\n<pre>package example.abhiandriod.togglebuttonexample;\r\n\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.view.Menu;\r\nimport android.view.MenuItem;\r\nimport android.view.View;\r\nimport android.widget.Button;\r\nimport android.widget.Toast;\r\nimport android.widget.ToggleButton;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    ToggleButton simpleToggleButton1, simpleToggleButton2;\r\n    Button submit;\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        \/\/ initiate toggle button's\r\n        simpleToggleButton1 = (ToggleButton) findViewById(R.id.simpleToggleButton1);\r\n        simpleToggleButton2 = (ToggleButton) findViewById(R.id.simpleToggleButton2);\r\n        submit = (Button) findViewById(R.id.submitButton);\r\n        submit.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View view) {\r\n                String status = \"ToggleButton1 : \" + simpleToggleButton1.getText() + \"\\n\" + \"ToggleButton2 : \" + simpleToggleButton2.getText();\r\n                Toast.makeText(getApplicationContext(), status, Toast.LENGTH_SHORT).show(); \/\/ display the current state of toggle button's\r\n            }\r\n        });\r\n    }\r\n\r\n    \r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<p>Now start the AVD in Emulator and run the App. You will see two ToggleButton and submit Button. Click on the submit button which will display the state of ToggleButton.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-954\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/ToggleButton-Example-in-Android-Studio-183x300.jpg\" alt=\"ToggleButton Example in Android Studio\" width=\"183\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/ToggleButton-Example-in-Android-Studio-183x300.jpg 183w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/ToggleButton-Example-in-Android-Studio.jpg 199w\" sizes=\"auto, (max-width: 183px) 100vw, 183px\" \/><\/center><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Android, ToggleButton is used to display checked and unchecked state of a button. ToggleButton basically an off\/on button with a light indicator which\u00a0indicate the current state of toggle button. The most simple example of ToggleButton is doing\u00a0on\/off in sound, Bluetooth, wifi, hotspot etc.\u00a0It\u00a0is a subclass of compoundButton. ToggleButton Vs Switch In Android: ToggleButton allow &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/togglebutton\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">ToggleButton (On\/Off) Tutorial With Example In Android<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-859","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>ToggleButton (On\/Off) Tutorial With Example In Android \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Learn ToggleButton with example in Android Studio which display checked and unchecked state of a button. Also find details of ToggleButton vs Switch, current state and its attributes.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/togglebutton\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/859","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/comments?post=859"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/859\/revisions"}],"predecessor-version":[{"id":2805,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/859\/revisions\/2805"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=859"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}