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:

1
2
3
4
5
6
<!--?xml version="1.0" encoding="utf-8"?-->
<resources>
  <color name="white">#FFFFFF</color>
  <color name="lightyellow">#FFFFE0</color>
 
</resources>

3. Create the Layout

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

1
2
<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">
</textview>

Here, the onClick event handler toggleWifi is declared in the XML and will be implemented in the corresponding Activity.

4. Edit the associated Activity

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//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:

01
02
03
04
05
06
07
08
09
10
11
12
// 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