<h3><strong><span style="color: #000080;">Toggle Buttons</span></strong></h3>
<p>A toggle button allows the user to change a setting between two states.</p>
<p>You can add a basic toggle button to your layout with the <span style="color: #008000;"><strong>ToggleButton</strong></span> object. Android 4.0 (API level 14) introduces another kind of toggle button called a<strong><span style="color: #008000;"> switch</span></strong> that provides a slider control, which you can add with a <strong><span style="color: #008000;">Switch</span></strong> object.</p>
<p>Android <strong><span style="color: #008000;">ToggleButton</span></strong> and <span style="color: #008000;"><strong>Switch</strong></span> both are the subclasses of <strong><span style="color: #008000;">CompoundButton</span> </strong>class.</p>
<p>Get <span style="color: #0000ff;"><strong>GITHUB</strong></span> code from <span style="color: #0000ff;"><a style="color: #0000ff;" href="https://github.com/arunk7839/ToggleButtonExp"><strong>HERE</strong></a></span>.</p>
<p><span class="embed-youtube" style="text-align:center; display: block;"><amp-youtube data-videoid="dgb7xGWs0ZY" data-param-rel="1" data-param-showsearch="0" data-param-showinfo="1" data-param-iv_load_policy="1" data-param-fs="1" data-param-hl="en-US" data-param-autohide="2" data-param-wmode="transparent" width="1200" height="675" layout="responsive"><a href="https://www.youtube.com/watch?v=dgb7xGWs0ZY" placeholder><amp-img src="https://i.ytimg.com/vi/dgb7xGWs0ZY/hqdefault.jpg" alt="YouTube Poster" layout="fill" object-fit="cover"><noscript><img src="https://i.ytimg.com/vi/dgb7xGWs0ZY/hqdefault.jpg" loading="lazy" decoding="async" alt="YouTube Poster"></noscript></amp-img></a></amp-youtube></span></p>
<p> ;</p>
<h3><strong><span style="color: #000080;">Creating New Project</span></strong></h3>
<ol>
<li>In Android Studio, go to <strong>File </strong><strong>⇒</strong><strong> New Project</strong> and fill all the details required to create a new project. When it prompts to select a default activity, select <strong>Blank Activity</strong> and proceed.</p>
</li>
<li>
<p>In <span style="color: #008000;"><strong>activity_main.xml</strong></span> I have add one <strong><span style="color: #008000;">ToggleButton</span></strong> with following basic properties.</p>
</li>
</ol>
<p><span style="color: #0000ff;"><strong>activity_main.xml</strong></span><br />
<code></code></p>
<pre><strong><;?xml version="1.0" encoding="utf-8"?>;</strong>
<strong><;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"</strong>
<strong> xmlns:tools="http://schemas.android.com/tools"</strong>
<strong> android:layout_width="match_parent"</strong>
<strong> android:layout_height="match_parent"</strong>
<strong> tools:context="com.example.lenovo.togglebuttonapp.MainActivity">;</strong>


<strong> <;ToggleButton</strong>
<strong> android:id="@+id/toggle_btn"</strong>
<strong> android:layout_width="wrap_content"</strong>
<strong> android:layout_height="wrap_content"</strong>
<strong> android:layout_centerInParent="true"</strong>
<strong> android:background="@android:color/holo_red_light"</strong>
<strong> android:padding="30dp"</strong>
<strong> android:textOff="DISABLE"</strong>
<strong> android:textOn="ENABLE"</strong>
<strong> android:textSize="25sp"</strong>
<strong> android:textStyle="bold" />;</strong>

<strong><;/RelativeLayout>;</strong>

</pre>
<p><span style="color: #0000ff;"><strong>XML Attributes of ToggleButton class used:</strong></span></p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>

<p><strong><span style="color: #008000;">android:textOff</span></strong> : The text for the button when it is off.</p>
<p><strong><span style="color: #008000;">android:textOn</span></strong> : The text for the button when it is on.</p>
<p>3.Now open <strong><span style="color: #008000;">MainActivity.java</span></strong> and write the below code.</p>
<p>To detect when the user activates the button , create an <strong><span style="color: #008000;">CompoundButton.OnCheckedChangeListener</span></strong> object and assign it to the button by calling <span style="color: #008000;"><strong>setOnCheckedChangeListener().</strong></span></p>
<p><span style="color: #0000ff;"><strong>MainActivity.java</strong></span><br />
<code></code></p>
<pre><strong>package com.example.lenovo.togglebuttonapp;</strong>

<strong>import android.graphics.Color;</strong>
<strong>import android.support.v7.app.AppCompatActivity;</strong>
<strong>import android.os.Bundle;</strong>
<strong>import android.widget.CompoundButton;</strong>
<strong>import android.widget.Toast;</strong>
<strong>import android.widget.ToggleButton;</strong>

<strong>public class MainActivity extends AppCompatActivity {</strong>

<strong> @Override</strong>
<strong> protected void onCreate(Bundle savedInstanceState) {</strong>
<strong> super.onCreate(savedInstanceState);</strong>
<strong> setContentView(R.layout.activity_main);</strong>

<strong> final ToggleButton toggle = (ToggleButton) findViewById(R.id.toggle_btn);</strong>

<strong> toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {</strong>
<strong> public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {</strong>

<strong> if (isChecked) {</strong>
<span style="color: #008000;"><strong> // The toggle is enabled</strong></span>
<strong> toggle.setBackgroundColor(Color.GREEN);</strong>
<strong> Toast.makeText(getApplicationContext(), "ToggleButton is ON ", Toast.LENGTH_LONG).show();</strong>
<strong> } else {</strong>
<span style="color: #008000;"><strong> // The toggle is disabled</strong></span>
<strong> toggle.setBackgroundColor(Color.RED);</strong>
<strong> Toast.makeText(getApplicationContext(), "ToggleButton is OFF ", Toast.LENGTH_LONG).show();</strong>
<strong> }</strong>
<strong> }</strong>
<strong> });</strong>
<strong> }</strong>
<strong>}</strong>


</pre>
<p><strong><span style="color: #008000;">Now when you run the app it will look like this:</span></strong></p>
<p><img class="alignnone wp-image-1532" src="https://c1ctech.com/wp-content/uploads/2020/02/Screenshot_1580911231.png" alt="Screenshot_1580911231" width="317" height="563" /> <img class="alignnone wp-image-1533" src="https://c1ctech.com/wp-content/uploads/2020/02/Screenshot_1580911223.png" alt="Screenshot_1580911223" width="316" height="561" /></p>
<p> ;</p>
<p> ;</p>


