Over a million developers have joined DZone.

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

DZone 's Guide to

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

· Java Zone ·
Free Resource

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 !

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}