Android How do I correctly get the value from a Switch?

I’m creating a Android application which uses a Switch. I’m trying to listen for changes and get the value when changed. I have two questions when using switches:

  1. What action listener do I use?
  2. How do I get the the switch value?

  • Could not validate certificate signature?
  • Recycler view item fill up entire recycler view height after upgrading support library from “23.1.1” to “23.2.1”
  • Rotating image. Animation list or animated rotate? (Android)
  • Is it possible to get notified (e.g. email) when the Google Play Developer Console receives a crash or ANR?
  • Google Analytics for Android v2 Campaign Tracking Not Working
  • Android Browsing history leaving out some sites only
  • Related posts:

    How to make a splash screen (screen visible when app starts)?
    ADB not responding. You can wait more, or kill “adb.exe” … windows 8
    Difference in android layout designs between versions 2.3.3 and 4+
    Is it possible to always show the action bar when using adjustPan as soft input mode?
    What fees are involved in publishing an android app?
    How to prevent the Android Market from JPEG-compressing the Promotional Graphic PNG?
  • DrawerLayout getting stuck on swipe
  • Android RelativeLayout programmatically set centerInParent
  • Action bar for Android 2.1
  • Why implement onDestroy() if it is not guaranteed to be called?
  • Read/write .txt file with special characters
  • similar functionality like uitableview in iphone to in android?
  • 2 Solutions collect form web for “Android How do I correctly get the value from a Switch?”

    Switch s = (Switch) findViewById(;
    if (s != null) {
    /* ... */
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Toast.makeText(this, "The Switch is " + (isChecked ? "on" : "off"),
        if(isChecked) {
            //do stuff when Switch is ON
        } else {
            //do stuff when Switch if OFF

    Since it extends from CompoundButton (docs), you can use setOnCheckedChangeListener() to listen for changes; use isChecked() to get the current state of the button.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.