Android Tutorial: ToggleButton And Switch

Simple example of how to use Android ToggleButton And Switch.

Android ToggleButton and Switch allow user to change a setting between two states which are mutually exclusive.

Note: Switch is introduced in Android 4.0 (API level 14), so it is available in Android 4.0+ (API level 14+).

Add a ToggleButton and a Switch to Activity layout: res/layout/activity_uicontrols_switch.xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.codevoila.androidtuts.uicontrols.SwitchActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Switch: "/>

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GPS"
        android:id="@+id/switch_gps"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ToggleButton: "/>

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/togglebutton_network"
        android:textOn="Connect"
        android:textOff="Disconnect"/>
</LinearLayout>

Add OnCheckedChangeListener separately to the ToggleButton and Switch in onCreate() method of the Activity.

/* SwitchActivity.java */

public class SwitchActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uicontrols_switch);

        Switch gpsSwitch = (Switch) this.findViewById(R.id.switch_gps);
        gpsSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                String status = isChecked ? "GPS is ON" : "GPS is OFF";
                Toast.makeText(SwitchActivity.this, status, Toast.LENGTH_LONG).show();
            }
        });

        ToggleButton networkToggleButton = (ToggleButton) this.findViewById(R.id.togglebutton_network);
        networkToggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                String status = isChecked ? "Network is connected" : "Network is disconnected";
                Toast.makeText(SwitchActivity.this, status, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Screenshot of running results.

android-togglebutton-switch-example

Notice that the full qualified name of the added OnCheckedChangeListener is android.widget.CompoundButton.OnCheckedChangeListener. So why we add a CompoundButton.OnCheckedChangeListener to ToggleButton and Switch? That's because both ToggleButton and Switch are inherited from android.widget.CompoundButton class.

android-togglebutton-switch-compoundbutton

Comments