Android Core

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:



  #FFFFFF
  #FFFFE0


3. Create the Layout

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

            

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 !

Reference: Android Tips: On/Off Toggle from our JCG partner Tony Sicilian at the Tony’s Blog blog.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button