Over a million developers have joined DZone.

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

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

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:


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"?>
  <color name="white">#FFFFFF</color>
  <color name="lightyellow">#FFFFE0</color>
<!-- other colors here  -->

3. Create the Layout

It will be just a clickable TextView with its associated Drawable:

<!-- WI_FI ON, OFF -->           

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;
protected void onCreate(Bundle savedInstanceState) {
   tvWifi = (TextView) findViewById(R.id.wifi_onoff);
   // get saved wifi status from db
   checkWifi = ...
/** onclick handler */
public void toggleWifi(View view) {
  setWifi( ! checkWifi);
/** Sets Wi-fi status + visual feedback */
private void setWifi(boolean onoff){
    onoff ?
    IC_WIFI_ON :
    0, 0, 0 );
  tvWifi.setTextColor( onoff ?
                       getResources().getColor(COLOR_ON) :
  // process.. enable or not
  WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
protected void onPause() {
  // save checkWifi flag in db

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


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
public void onCreate(Bundle savedInstanceState) {
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 MotionEvents.

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 !

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.


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

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}