DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • What Is Envoy Proxy?
  • From On-Prem to SaaS
  • Cucumber Selenium Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Integrate Cucumber in Playwright With Java

Trending

  • What Is Envoy Proxy?
  • From On-Prem to SaaS
  • Cucumber Selenium Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Integrate Cucumber in Playwright With Java
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Android Tips: Add an On/Off Toggle for a Widget

Android Tips: Add an On/Off Toggle for a Widget

Tony Siciliani user avatar by
Tony Siciliani
·
Nov. 19, 14 · Interview
Like (0)
Save
Tweet
Share
6.08K Views

Join the DZone community and get the full member experience.

Join For Free

when we need to give to users of our apps the ability to switch on/off a given feature, we automatically think of check boxes, toggle buttons or switches:

switches

these widgets are available to us out-of-the-box, but then we are constrained to a particular look & feel, which may or may not be what we want in our app. we could customize the switches, but if we’re looking for something entirely different, there are other ways to give users a visual feedback on whether a given feature is enabled or not, for example with plain text and a couple of icons:

wifi-off wifi-on

we could switch from one state to the other by simply touching the text field directly, instead of using buttons, switches or check boxes, with a consistent look & feel across all android versions. but how?


1. choose the on/off icons

if we don’t have the icons already, creating those using android asset studio ‘s icon generator is fast & easy.


2. choose the on/off colors

in our android project’s res/values/colors.xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <color name="white">#ffffff</color>
  <color name="lightyellow">#ffffe0</color>
<!-- other colors here  -->
</resources>

3. create the layout

it will be just a clickable textview with its associated drawable:

<!-- wi_fi on, off -->           
      <textview
        android:id="@+id/wifi_onoff"
        style="@style/..."
        android:text="@string/wifi_txt"  
        android:lines="1"
        android:gravity="center_vertical"
        android:drawableleft="@drawable/ic_wifi_off"
        android:clickable="true"
        android:onclick="togglewifi"/>

here, the onclick event handler togglewifi is declared in the xml and will be implemented in the corresponding activity.


4. edit the associated activity

//inside activity
//...
// field
private textview tvwifi;
// flag saved in prefs or db
private boolean checkwifi;
// colors
private static final int color_off = r.color.white;
private static final int color_on = r.color.lightyellow;
// icons
private static final int ic_wifi_off = r.drawable.ic_wifi_off;
private static final int ic_wifi_on = r.drawable.ic_wifi_on;
//...
@override
protected void oncreate(bundle savedinstancestate) {
   super.oncreate(savedinstancestate);
   //...
   tvwifi = (textview) findviewbyid(r.id.wifi_onoff);
   // get saved wifi status from db
   checkwifi = ...
   setwifi(checkwifi);
}
 
//...
 
/** onclick handler */
public void togglewifi(view view) {
 
  //toggle
  setwifi( ! checkwifi);
}
 
/** sets wi-fi status + visual feedback */
private void setwifi(boolean onoff){
 
  tvwifi.setcompounddrawableswithintrinsicbounds(
    onoff ?
    ic_wifi_on :
    ic_wifi_off,
    0, 0, 0 );
 
  tvwifi.settextcolor( onoff ?
                       getresources().getcolor(color_on) :
                       getresources().getcolor(color_off));
 
  // process.. enable or not
  wifimanager wifi = (wifimanager) getsystemservice(context.wifi_service);
  wifi.setwifienabled(onoff);
}
 
//...
@override
protected void onpause() {
  super.onpause();
  // save checkwifi flag in db
  //...
}
 
//...

we can do something similar for as many fields as we wish:

fields-on

notice that we used a view.onclicklistener while defining the onclick() in the xml layout. we could have used a view.ontouchlistener instead:

// activity now implements view.ontouchlistener
@override
public void oncreate(bundle savedinstancestate) {
   //...
   tvwifi.setontouchlistener(this);
}
//...
@override
public boolean ontouch(view v, motionevent event) {
   // handle event here ...
   return true;
}

what’s the difference between onclick and ontouch ? not much in this particular use case, since we do not need the extra fancy stuff we could do with ontouch and motionevent s.

this technique is just an alternative way for on/off toggle. we could also easily enhance it with some type of animation. whether it is “better” or not than regular switches, is mostly a question of taste. it is kind of “non-standard” (whatever that means), but, on the other hand, it might give designers more freedom and abilities to create their own original uis.

happy coding !
Android (robot)

Published at DZone with permission of Tony Siciliani, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • What Is Envoy Proxy?
  • From On-Prem to SaaS
  • Cucumber Selenium Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Integrate Cucumber in Playwright With Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: