Android Tips: On/Off Toggle

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 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 !

This article is also available on DZone Mobile and Java Code Geeks.

Advertisements

, , , , ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: