Site icon C1CTech

Android ToggleButton Example

<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Toggle Buttons<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>A toggle button allows the user to change a setting between two states&period;<&sol;p>&NewLine;<p>You can add a basic toggle button to your layout with the <span style&equals;"color&colon; &num;008000&semi;"><strong>ToggleButton<&sol;strong><&sol;span> object&period; Android 4&period;0 &lpar;API level 14&rpar; introduces another kind of toggle button called a<strong><span style&equals;"color&colon; &num;008000&semi;"> switch<&sol;span><&sol;strong> that provides a slider control&comma; which you can add with a <strong><span style&equals;"color&colon; &num;008000&semi;">Switch<&sol;span><&sol;strong> object&period;<&sol;p>&NewLine;<p>Android <strong><span style&equals;"color&colon; &num;008000&semi;">ToggleButton<&sol;span><&sol;strong> and <span style&equals;"color&colon; &num;008000&semi;"><strong>Switch<&sol;strong><&sol;span> both are the subclasses of <strong><span style&equals;"color&colon; &num;008000&semi;">CompoundButton<&sol;span> <&sol;strong>class&period;<&sol;p>&NewLine;<p>Get <span style&equals;"color&colon; &num;0000ff&semi;"><strong>GITHUB<&sol;strong><&sol;span> code from <span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;ToggleButtonExp"><strong>HERE<&sol;strong><&sol;a><&sol;span>&period;<&sol;p>&NewLine;<p><span class&equals;"embed-youtube" style&equals;"text-align&colon;center&semi; display&colon; block&semi;"><amp-youtube data-videoid&equals;"dgb7xGWs0ZY" data-param-rel&equals;"1" data-param-showsearch&equals;"0" data-param-showinfo&equals;"1" data-param-iv&lowbar;load&lowbar;policy&equals;"1" data-param-fs&equals;"1" data-param-hl&equals;"en-US" data-param-autohide&equals;"2" data-param-wmode&equals;"transparent" width&equals;"1200" height&equals;"675" layout&equals;"responsive"><a href&equals;"https&colon;&sol;&sol;www&period;youtube&period;com&sol;watch&quest;v&equals;dgb7xGWs0ZY" placeholder><amp-img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;dgb7xGWs0ZY&sol;hqdefault&period;jpg" alt&equals;"YouTube Poster" layout&equals;"fill" object-fit&equals;"cover"><noscript><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;dgb7xGWs0ZY&sol;hqdefault&period;jpg" loading&equals;"lazy" decoding&equals;"async" alt&equals;"YouTube Poster"><&sol;noscript><&sol;amp-img><&sol;a><&sol;amp-youtube><&sol;span><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Creating New Project<&sol;span><&sol;strong><&sol;h3>&NewLine;<ol>&NewLine;<li>In Android Studio&comma; go to <strong>File <&sol;strong><strong>&Implies;<&sol;strong><strong> New Project<&sol;strong> and fill all the details required to create a new project&period; When it prompts to select a default activity&comma; select <strong>Blank Activity<&sol;strong> and proceed&period;<&sol;p>&NewLine;<&sol;li>&NewLine;<li>&NewLine;<p>In <span style&equals;"color&colon; &num;008000&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span>  I have add  one <strong><span style&equals;"color&colon; &num;008000&semi;">ToggleButton<&sol;span><&sol;strong> with following basic properties&period;<&sol;p>&NewLine;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span><br &sol;>&NewLine;<code><&sol;code><&sol;p>&NewLine;<pre><strong>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;<&sol;strong>&NewLine;<strong>&lt&semi;RelativeLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<&sol;strong>&NewLine;<strong> xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;height&equals;"match&lowbar;parent"<&sol;strong>&NewLine;<strong> tools&colon;context&equals;"com&period;example&period;lenovo&period;togglebuttonapp&period;MainActivity"&gt&semi;<&sol;strong>&NewLine;&NewLine;&NewLine;<strong> &lt&semi;ToggleButton<&sol;strong>&NewLine;<strong> android&colon;id&equals;"&commat;&plus;id&sol;toggle&lowbar;btn"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<&sol;strong>&NewLine;<strong> android&colon;layout&lowbar;centerInParent&equals;"true"<&sol;strong>&NewLine;<strong> android&colon;background&equals;"&commat;android&colon;color&sol;holo&lowbar;red&lowbar;light"<&sol;strong>&NewLine;<strong> android&colon;padding&equals;"30dp"<&sol;strong>&NewLine;<strong> android&colon;textOff&equals;"DISABLE"<&sol;strong>&NewLine;<strong> android&colon;textOn&equals;"ENABLE"<&sol;strong>&NewLine;<strong> android&colon;textSize&equals;"25sp"<&sol;strong>&NewLine;<strong> android&colon;textStyle&equals;"bold" &sol;&gt&semi;<&sol;strong>&NewLine;&NewLine;<strong>&lt&semi;&sol;RelativeLayout&gt&semi;<&sol;strong>&NewLine;&NewLine;<&sol;pre>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>XML Attributes of ToggleButton class used&colon;<&sol;strong><&sol;span><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">android&colon;textOff<&sol;span><&sol;strong>  &colon;  The text for the button when it is off&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">android&colon;textOn<&sol;span><&sol;strong>  &colon;   The text for the button when it is on&period;<&sol;p>&NewLine;<p>3&period;Now open  <strong><span style&equals;"color&colon; &num;008000&semi;">MainActivity&period;java<&sol;span><&sol;strong> and write the below code&period;<&sol;p>&NewLine;<p>To detect when the user activates the button &comma; create an <strong><span style&equals;"color&colon; &num;008000&semi;">CompoundButton&period;OnCheckedChangeListener<&sol;span><&sol;strong> object and assign it to the button by calling <span style&equals;"color&colon; &num;008000&semi;"><strong>setOnCheckedChangeListener&lpar;&rpar;&period;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span><br &sol;>&NewLine;<code><&sol;code><&sol;p>&NewLine;<pre><strong>package com&period;example&period;lenovo&period;togglebuttonapp&semi;<&sol;strong>&NewLine;&NewLine;<strong>import android&period;graphics&period;Color&semi;<&sol;strong>&NewLine;<strong>import android&period;support&period;v7&period;app&period;AppCompatActivity&semi;<&sol;strong>&NewLine;<strong>import android&period;os&period;Bundle&semi;<&sol;strong>&NewLine;<strong>import android&period;widget&period;CompoundButton&semi;<&sol;strong>&NewLine;<strong>import android&period;widget&period;Toast&semi;<&sol;strong>&NewLine;<strong>import android&period;widget&period;ToggleButton&semi;<&sol;strong>&NewLine;&NewLine;<strong>public class MainActivity extends AppCompatActivity &lbrace;<&sol;strong>&NewLine;&NewLine;<strong> &commat;Override<&sol;strong>&NewLine;<strong> protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;<&sol;strong>&NewLine;<strong> super&period;onCreate&lpar;savedInstanceState&rpar;&semi;<&sol;strong>&NewLine;<strong> setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;<&sol;strong>&NewLine;&NewLine;<strong> final ToggleButton toggle &equals; &lpar;ToggleButton&rpar; findViewById&lpar;R&period;id&period;toggle&lowbar;btn&rpar;&semi;<&sol;strong>&NewLine;&NewLine;<strong> toggle&period;setOnCheckedChangeListener&lpar;new CompoundButton&period;OnCheckedChangeListener&lpar;&rpar; &lbrace;<&sol;strong>&NewLine;<strong> public void onCheckedChanged&lpar;CompoundButton buttonView&comma; boolean isChecked&rpar; &lbrace;<&sol;strong>&NewLine;&NewLine;<strong> if &lpar;isChecked&rpar; &lbrace;<&sol;strong>&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; The toggle is enabled<&sol;strong><&sol;span>&NewLine;<strong> toggle&period;setBackgroundColor&lpar;Color&period;GREEN&rpar;&semi;<&sol;strong>&NewLine;<strong> Toast&period;makeText&lpar;getApplicationContext&lpar;&rpar;&comma; "ToggleButton is ON "&comma; Toast&period;LENGTH&lowbar;LONG&rpar;&period;show&lpar;&rpar;&semi;<&sol;strong>&NewLine;<strong> &rcub; else &lbrace;<&sol;strong>&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol; The toggle is disabled<&sol;strong><&sol;span>&NewLine;<strong> toggle&period;setBackgroundColor&lpar;Color&period;RED&rpar;&semi;<&sol;strong>&NewLine;<strong> Toast&period;makeText&lpar;getApplicationContext&lpar;&rpar;&comma; "ToggleButton is OFF "&comma; Toast&period;LENGTH&lowbar;LONG&rpar;&period;show&lpar;&rpar;&semi;<&sol;strong>&NewLine;<strong> &rcub;<&sol;strong>&NewLine;<strong> &rcub;<&sol;strong>&NewLine;<strong> &rcub;&rpar;&semi;<&sol;strong>&NewLine;<strong> &rcub;<&sol;strong>&NewLine;<strong>&rcub;<&sol;strong>&NewLine;&NewLine;&NewLine;<&sol;pre>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">Now when you run the app it will look like this&colon;<&sol;span><&sol;strong><&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-1532" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;02&sol;Screenshot&lowbar;1580911231&period;png" alt&equals;"Screenshot&lowbar;1580911231" width&equals;"317" height&equals;"563" &sol;>    <img class&equals;"alignnone wp-image-1533" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;02&sol;Screenshot&lowbar;1580911223&period;png" alt&equals;"Screenshot&lowbar;1580911223" width&equals;"316" height&equals;"561" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;&NewLine;

Exit mobile version