{"id":861,"date":"2016-02-11T09:37:02","date_gmt":"2016-02-11T09:37:02","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=861"},"modified":"2018-06-05T10:37:50","modified_gmt":"2018-06-05T10:37:50","slug":"switch","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/switch","title":{"rendered":"Switch (On\/Off) Tutorial With Example In Android Studio"},"content":{"rendered":"<p>In Android, Switch is a two-state toggle switch widget that can select between two options. It is used to display checked and unchecked state of a button providing slider control to user. Switch is a subclass of CompoundButton. It is basically an off\/on button which indicate the current state of Switch. It is commonly used in selecting on\/off in Sound, Bluetooth, WiFi etc.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-935 size-full\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/Switch-Android.jpg\" alt=\"Switch Android\" width=\"526\" height=\"203\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Switch-Android.jpg 526w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Switch-Android-300x116.jpg 300w\" sizes=\"auto, (max-width: 526px) 100vw, 526px\" \/><\/center>As compared to ToggleButton Switch provides user slider control. The user can simply tap on a switch to change its current state.<\/p>\n<p>Switch allow the users to change the setting between two states like turn on\/off \u00a0wifi, Bluetooth etc from your phone\u2019s setting menu. It was introduced after Android 4.0 version (API level 14).<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note: <\/strong><\/span>Android Switch and ToggleButton both are the subclasses of CompoundButton class.<\/p>\n<p><strong>Switch code in XML:<\/strong><\/p>\n<pre>&lt;Switch\r\nandroid:id=\"@+id\/simpleSwitch\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\/&gt;\r\n<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-909\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/Switch-in-Android.jpg\" alt=\"Switch in Android\" width=\"328\" height=\"176\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Switch-in-Android.jpg 328w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Switch-in-Android-300x161.jpg 300w\" sizes=\"auto, (max-width: 328px) 100vw, 328px\" \/><\/center><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> We\u00a0can check the current state of a Switch programmatically by using isChecked() method. This method returns a Boolean value means true or false. If a Switch is checked then it returns true otherwise it returns false.<\/p>\n<p>Below is an example code in which we checked the current state of a Switch.<\/p>\n<pre>\/\/ initiate a Switch\r\nSwitch simpleSwitch = (Switch) findViewById(R.id.<em>simpleSwitch<\/em>);\r\n\r\n\/\/ check current state of a Switch (true or false).\r\nBoolean switchState = simpleSwitch.isChecked();\r\n<\/pre>\n<hr \/>\n<h4><strong>Attributes of Switch:<\/strong><\/h4>\n<p>Now let\u2019s \u00a0we discuss important Switch attributes that helps us to configure a Switch in XML file(layout).<\/p>\n<p><span style=\"color: #008000;\"><strong>1. id:<\/strong><\/span> id is an attribute used to uniquely identify a Switch.<\/p>\n<pre>&lt;Switch\r\n<strong>android:id=\"@+id\/simpleSwitch\"<\/strong>\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\/&gt;<\/pre>\n<p><strong><span style=\"color: #008000;\">2. checked:<\/span>\u00a0<\/strong>checked attribute of Switch is used to set the current state of a Switch. The value can\u00a0be either true or false where true shows the checked state and false shows unchecked state of a Switch. The default value of checked attribute is false. We can also set the current state programmatically.<\/p>\n<p>Below we set &#8220;true&#8221; value for checked attribute that sets the current state to checked.<\/p>\n<pre>&lt;Switch\r\nandroid:id=\"@+id\/simpleSwitch\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:checked=\"true\"\/&gt; &lt;!-- set the current state of the Switch--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-910\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/Checked-Attribute-in-Switch-Android.jpg\" alt=\"Checked Attribute in Switch Android\" width=\"320\" height=\"232\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Checked-Attribute-in-Switch-Android.jpg 320w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Checked-Attribute-in-Switch-Android-300x218.jpg 300w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/center><strong>Setting Check Attribute In Switch Using Java Class:<\/strong><\/p>\n<p>Below we set the current state of Switch in java class.<\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\n\/\/ initiate a Switch\r\nSwitch simpleSwitch = (Switch) findViewById(R.id.<em>simpleSwitch<\/em>);\r\n\r\n\/\/set the current state of a Switch\r\nsimpleSwitch.setChecked(true);<\/pre>\n<p><span style=\"color: #008000;\"><strong>3. text:<\/strong><\/span> text attribute is used to set the text in a Switch. We can set the text in xml as well as in the java class.<\/p>\n<p>Below we set the text \u201cSound\u201d for the Switch.<\/p>\n<pre>&lt;Switch\r\nandroid:id=\"@+id\/simpleSwitch\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_centerHorizontal=\"true\"\r\nandroid:text=\"Sound\" \/&gt; &lt;!--displayed text of switch--&gt;\r\n<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-911\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/text-attribute-in-Switch-Android.jpg\" alt=\"text attribute in Switch Android\" width=\"315\" height=\"224\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/text-attribute-in-Switch-Android.jpg 315w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/text-attribute-in-Switch-Android-300x213.jpg 300w\" sizes=\"auto, (max-width: 315px) 100vw, 315px\" \/><\/center><strong>Setting Text In Switch Using Java class:<\/strong><\/p>\n<p>In the below code we set text of\u00a0Switch via java class.<\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\n\/\/ initiate Switch\r\nSwitch simpleSwitch = (Switch) findViewById(R.id.<em>simpleSwitch<\/em>);\r\n\r\n\/\/displayed text of the Switch\r\nsimpleSwitch.setText(\"switch\");\r\n<\/pre>\n<p><strong><span style=\"color: #008000;\">4. gravity:<\/span>\u00a0<\/strong>The gravity attribute is an optional attribute which is used to control the alignment of the text in Switch. We can set text\u00a0left, right, center, top, bottom, center_vertical, center_horizontal etc in Switch.<\/p>\n<p>In the below code we set the left\u00a0gravity for the text in Switch.<\/p>\n<pre>&lt;Switch\r\n    android:id=\"@+id\/simpleSwitch\"\r\n    android:layout_width=\"fill_parent\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:text=\"Sound\"\r\n    android:checked=\"true\"\r\n    android:gravity=\"left\"\/&gt;&lt;!--gravity of the Switch--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-912\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/gravity-in-Switch-Android.jpg\" alt=\"gravity in Switch Android\" width=\"317\" height=\"235\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/gravity-in-Switch-Android.jpg 317w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/gravity-in-Switch-Android-300x222.jpg 300w\" sizes=\"auto, (max-width: 317px) 100vw, 317px\" \/><\/center><strong><span style=\"color: #008000;\">5. textOn And testOff:<\/span>\u00a0<\/strong>textOn attribute is used to set the text when Switch is in checked state (i.e. on state). We can set the textOn in XML\u00a0as well as in the java class.<\/p>\n<p>In the below example\u00a0we set the textOn as &#8220;Yes&#8221; and textOff as &#8220;No&#8221; for a Switch.<\/p>\n<pre>&lt;Switch\r\n    android:id=\"@+id\/simpleSwitch\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:checked=\"true\"\r\n    android:text=\"Notification\"\r\n    android:textOff=\"No\"\r\n    android:textOn=\"Yes\"\/&gt;&lt;!-- text to be displayed whenever current state is checked--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-931\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/texton-textoff-in-Switch-Android.jpg\" alt=\"texton textoff in Switch Android\" width=\"617\" height=\"140\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/texton-textoff-in-Switch-Android.jpg 617w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/texton-textoff-in-Switch-Android-300x68.jpg 300w\" sizes=\"auto, (max-width: 617px) 100vw, 617px\" \/><\/center><strong>Setting textOn And testOff of\u00a0Switch In Java class:<\/strong><\/p>\n<p>Below we set the textOn and textOff of a Switch in java class.<\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\nSwitch simpleSwitch = (Switch) findViewById(R.id.<strong><em>simpleSwitch<\/em><\/strong>); \/\/ initiate Switch\r\n\r\nsimpleSwitch.setTextOn(<strong>\"On\"<\/strong>); \/\/ displayed text of the Switch whenever it is in checked or on state\r\nsimpleSwitch.setTextOff(\"Off\"); \/\/ displayed text of the Switch whenever it is in unchecked i.e. off state<\/pre>\n<p><strong><span style=\"color: #008000;\">6. textColor:<\/span>\u00a0<\/strong>textColor attribute is used to set the text color of a Switch. Color value is in the form of &#8220;#argb&#8221;, &#8220;#rgb&#8221;, &#8220;#rrggbb&#8221;, or &#8220;#aarrggbb&#8221;.<\/p>\n<p>Below is the example code with explanation included in which we set the red color for the displayed text of a Switch.<\/p>\n<pre>&lt;Switch\r\n    android:id=\"@+id\/simpleSwitch\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:checked=\"true\"\r\n    android:text=\"switch\"\r\n    android:layout_centerHorizontal=\"true\"\r\n    android:textOn=\"On\"\r\n    android:textOff=\"Off\"\r\n    android:gravity=\"center\"\r\n    android:textColor=\"#008F36\"\/&gt;&lt;!-- Green color for displayed text--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-925\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/textColor-Attribute-in-Switch-300x191.jpg\" alt=\"textColor Attribute in Switch\" width=\"300\" height=\"191\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/textColor-Attribute-in-Switch-300x191.jpg 300w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/textColor-Attribute-in-Switch.jpg 305w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/center><strong>Setting textColor Of Switch text In Java class:<\/strong><\/p>\n<p>Below is the example code in which we set the text color of a Switch programmatically.<\/p>\n<pre>\/*Add in Oncreate() funtion after setContentView()*\/\r\n Switch simpleSwitch = (Switch) findViewById(R.id.simpleSwitch);\/\/ initiate Switch\r\n\r\n simpleSwitch.setTextColor(Color.GREEN); \/\/red color for displayed text of Switch<\/pre>\n<p><span style=\"color: #008000;\"><strong>7. textSize:<\/strong><\/span>\u00a0textSize attribute is used to set the size of the text of a Switch. We can set the text size in sp(scale independent pixel) or dp(density pixel).<\/p>\n<p>Below is the example code in which we set the 25sp size for the text of a Switch.<\/p>\n<pre>&lt;Switch\r\n    android:id=\"@+id\/simpleSwitch\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:checked=\"true\"\r\n    android:text=\"switch\"\r\n    android:layout_centerHorizontal=\"true\"\r\n    android:textOn=\"On\"\r\n    android:textOff=\"Off\"\r\n    android:textColor=\"#f00\"\r\n    android:gravity=\"center\"\r\n    android:textSize=\"25sp\"\/&gt; &lt;!--25sp displayed text size--&gt;<\/pre>\n<p><center>\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-926\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/textSize-in-Switch-Android-300x218.jpg\" alt=\"textSize in Switch Android\" width=\"300\" height=\"218\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/textSize-in-Switch-Android-300x218.jpg 300w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/textSize-in-Switch-Android.jpg 303w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/center><strong>Setting textSize Of Switch Text In Java class:<\/strong><\/p>\n<p>Below is the example code in which we set the text size of a Switch programmatically.<\/p>\n<pre>Switch simpleSwitch = (Switch) findViewById(R.id.<strong><em>simpleSwitch<\/em><\/strong>); \/\/ initiate Switch\r\n\r\nsimpleSwitch.setTextSize(25); \/\/ set 25sp displayed text size of Switch<\/pre>\n<p><span style=\"color: #008000;\"><strong>9. textStyle:<\/strong><\/span>\u00a0textStyle attribute is used to set the text style of the text of a Switch. The possible text styles are bold, italic and normal.\u00a0 If we need to use two or more styles for a text view then use \u201c|\u201d operator.<\/p>\n<p>Below we set the bold and italic text styles for text of a Switch.<\/p>\n<pre>&lt;Switch\r\n    android:id=\"@+id\/simpleSwitch\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:checked=\"false\"\r\n    android:text=\"switch\"\r\n    android:layout_centerHorizontal=\"true\"\r\n    android:textOn=\"On\"\r\n    android:textOff=\"Off\"\r\n    android:textColor=\"#f00\"\r\n    android:gravity=\"center\"\r\n    android:textSize=\"25sp\"\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-medium wp-image-927\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/textStyle-in-Switch-Android-300x203.jpg\" alt=\"textStyle in Switch Android\" width=\"300\" height=\"203\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/textStyle-in-Switch-Android-300x203.jpg 300w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/textStyle-in-Switch-Android.jpg 306w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/center><span style=\"color: #008000;\"><strong>10. background:<\/strong><\/span>\u00a0background attribute is used to set the background of a Switch. We can set a color or a drawable in the background of a Switch.<\/p>\n<p>Below we set the black color for the background and red color for the displayed text of a Switch.<\/p>\n<pre>&lt;Switch\r\n    android:id=\"@+id\/simpleSwitch\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:checked=\"true\"\r\n    android:text=\"switch\"\r\n    android:layout_centerHorizontal=\"true\"\r\n    android:textOn=\"On\"\r\n    android:textOff=\"Off\"\r\n    android:textColor=\"#f00\"\r\n    android:padding=\"20dp\"\r\n    android:gravity=\"center\"\r\n    android:textSize=\"25sp\"\r\n    android:background=\"#000\"\/&gt;&lt;!-- black background color--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-928\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/Background-in-Switch-Android-300x245.jpg\" alt=\"Background in Switch Android\" width=\"300\" height=\"245\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Background-in-Switch-Android-300x245.jpg 300w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Background-in-Switch-Android.jpg 306w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/center><strong>Setting Background In Java class:<\/strong><\/p>\n<p>Below we set the background color of a Switch programmatically.<\/p>\n<pre>Switch simpleSwitch = (Switch) findViewById(R.id.<em>simpleSwitch<\/em>);\r\nsimpleSwitch.setBackgroundColor(Color.<em>BLACK<\/em>);<\/pre>\n<p><span style=\"color: #008000;\"><strong>11. padding:<\/strong><\/span> padding attribute is used to set the padding from left, right, top or bottom in Switch.<\/p>\n<ul>\n<li><strong>paddingRight:<\/strong>\u00a0set the padding from the right side of the Switch<strong>.<\/strong><\/li>\n<li><strong>paddingLeft:<\/strong>\u00a0set the padding from the left side of the Switch<strong>.<\/strong><\/li>\n<li><strong>paddingTop:<\/strong>\u00a0set the padding from the top side of the Switch<strong>.<\/strong><\/li>\n<li><strong>paddingBottom:<\/strong>\u00a0set the padding from the bottom side of the Switch<strong>.<\/strong><\/li>\n<li><strong>Padding:<\/strong>\u00a0set the padding from the all side\u2019s of the Switch<strong>.<\/strong><\/li>\n<\/ul>\n<p>Below we set the 20dp padding from all the side\u2019s of the Switch.<\/p>\n<pre>&lt;Switch\r\n    android:id=\"@+id\/simpleSwitch\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:checked=\"true\"\r\n    android:text=\"switch\"\r\n    android:layout_centerHorizontal=\"true\"\r\n    android:textOn=\"On\"\r\n    android:textOff=\"Off\"\r\n    android:textColor=\"#f00\"\r\n    android:gravity=\"center\"\r\n    android:textSize=\"25sp\"\r\n    android:padding=\"40dp\"\/&gt;&lt;!-- 20dp padding from all the side's--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-929\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/Padding-in-Switch-Android-300x243.jpg\" alt=\"Padding in Switch Android\" width=\"300\" height=\"243\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Padding-in-Switch-Android-300x243.jpg 300w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Padding-in-Switch-Android.jpg 303w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/center><span style=\"color: #008000;\"><strong>12. drawableBottom, drawableTop, drawableRight And drawableLeft:<\/strong><\/span>\u00a0These attribute draw the drawable below, top, right and left\u00a0of the text of Switch.<\/p>\n<p>Below we set the icon to the below of the text of a Switch. You can try other three by yourself.<\/p>\n<p>Before you try this make sure you have icon image in drawable folder<\/p>\n<pre>&lt;Switch\r\n    android:id=\"@+id\/simpleSwitch\"\r\n    android:layout_width=\"wrap_content\"\r\n    android:layout_height=\"wrap_content\"\r\n    android:checked=\"true\"\r\n    android:text=\"switch\"\r\n    android:layout_centerHorizontal=\"true\"\r\n    android:textOn=\"On\"\r\n    android:textOff=\"Off\"\r\n    android:textColor=\"#f00\"\r\n    android:gravity=\"center\"\r\n    android:textSize=\"25sp\"\r\n    android:drawableBottom=\"@drawable\/ic_launcher\"\/&gt;&lt;!--drawable icon to be displayed in the bottom of text--&gt;<\/pre>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-930\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/drawable-in-Switch-Android-300x231.jpg\" alt=\"drawable in Switch Android\" width=\"300\" height=\"231\" \/><\/center><\/p>\n<hr \/>\n<h4><strong>Example of Switch In Android Studio:<\/strong><\/h4>\n<p>Below is the example of Switch in Android Studio. In this example, we display two Switches and one &#8220;submit&#8221; button\u00a0using\u00a0background &amp;\u00a0other attributes as discussed earlier in this post.\u00a0Whenever you click on submit button the current state for both the Switch\u2019s is displayed in a Toast. Below is the final output, code and example explained step by step:<\/p>\n<p style=\"text-align: center;\">[cp_modal id=&#8221;cp_id_5e2fa&#8221;]<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2150\" src=\"\/ui\/wp-content\/uploads\/2016\/08\/Download-Code1.png\" alt=\"Download Code\" width=\"439\" height=\"65\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/08\/Download-Code1.png 439w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/08\/Download-Code1-300x44.png 300w\" sizes=\"auto, (max-width: 439px) 100vw, 439px\" \/>[\/cp_modal]<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-933\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/Switch-Example-in-Android-Studio.jpg\" alt=\"Switch Example in Android Studio\" width=\"195\" height=\"355\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Switch-Example-in-Android-Studio.jpg 195w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Switch-Example-in-Android-Studio-165x300.jpg 165w\" sizes=\"auto, (max-width: 195px) 100vw, 195px\" \/><\/center><span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project and name it SwitchExample<\/p>\n<p>In this step we create a new project 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 Fill the forms and click \"Finish\" button.<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Open res -&gt; layout -&gt;\u00a0<strong>activity_main.xml (or) main.xml<\/strong>\u00a0and add following code:<\/p>\n<p>In this step we open an xml file and add the code for displaying two Switch 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\r\n    &lt;Switch\r\n        android:id=\"@+id\/simpleSwitch1\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_gravity=\"center\"\r\n        android:checked=\"false\"\r\n        android:text=\"switch 1\"\r\n        android:textOff=\"Off\"\r\n        android:textOn=\"On\" \/&gt;\r\n\r\n    &lt;Switch\r\n        android:id=\"@+id\/simpleSwitch2\"\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=\"20dp\"\r\n        android:checked=\"true\"\r\n        android:text=\"switch 2\"\r\n        android:textOff=\"Off\"\r\n        android:textOn=\"On\" \/&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=\"#009284\"\r\n        android:padding=\"10dp\"\r\n        android:text=\"Submit\"\r\n        android:textColor=\"#fff\"\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 two Switch&#8217;s and one normal button. And then we perform click event on button and display the text of current state of Switch by using a toast.<\/p>\n<pre>package example.abhiandriod.switchexample;\r\n\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.view.View;\r\nimport android.widget.Button;\r\nimport android.widget.Switch;\r\nimport android.widget.Toast;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    Switch simpleSwitch1, simpleSwitch2;\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 view's\r\n        simpleSwitch1 = (Switch) findViewById(R.id.simpleSwitch1);\r\n        simpleSwitch2 = (Switch) findViewById(R.id.simpleSwitch2);\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 statusSwitch1, statusSwitch2;\r\n                if (simpleSwitch1.isChecked())\r\n                    statusSwitch1 = simpleSwitch1.getTextOn().toString();\r\n                else\r\n                    statusSwitch1 = simpleSwitch1.getTextOff().toString();\r\n                if (simpleSwitch2.isChecked())\r\n                    statusSwitch2 = simpleSwitch2.getTextOn().toString();\r\n                else\r\n                    statusSwitch2 = simpleSwitch2.getTextOff().toString();\r\n                Toast.makeText(getApplicationContext(), \"Switch1 :\" + statusSwitch1 + \"\\n\" + \"Switch2 :\" + statusSwitch2, Toast.LENGTH_LONG).show(); \/\/ display the current state for switch's\r\n            }\r\n        });\r\n\r\n    }  \r\n}\r\n<\/pre>\n<p><strong>Output:\u00a0<\/strong><\/p>\n<p>Now start the AVD in Emulator and run the App. You will see two switch and one Button. Now change the state of the switch (i.e. either checked or unchecked) and click on the Button. You will see the state displayed on screen as Toast.<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-933\" src=\"\/ui\/wp-content\/uploads\/2016\/02\/Switch-Example-in-Android-Studio-165x300.jpg\" alt=\"Switch Example in Android Studio\" width=\"165\" height=\"300\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Switch-Example-in-Android-Studio-165x300.jpg 165w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/02\/Switch-Example-in-Android-Studio.jpg 195w\" sizes=\"auto, (max-width: 165px) 100vw, 165px\" \/><\/center><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Android, Switch is a two-state toggle switch widget that can select between two options. It is used to display checked and unchecked state of a button providing slider control to user. Switch is a subclass of CompoundButton. It is basically an off\/on button which indicate the current state of Switch. It is commonly used &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/switch\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Switch (On\/Off) Tutorial With Example In Android Studio<\/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-861","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>Switch (On\/Off) Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Learn how switch provide user control slider to toggle between checked and unchecked state of a button i.e. on\/off in Android with example. Details of its attributes like textOn and textOff in Switch.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/switch\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/861","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=861"}],"version-history":[{"count":2,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/861\/revisions"}],"predecessor-version":[{"id":2662,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/861\/revisions\/2662"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}