title: Toggle a switch based off dialog button choice in Android application
body: <p>I am attempting to build my first Android application, and I'm stuck already. I have two toggle switches, and when they're turned on, a dialog window appears. I would like the "Cancel" button to turn the switch back off. I've tried toggleButton.setChecked(false), Switch.setChecked(false), etc., but because these switches were created in an XML file, there's no object to execute that method on/with. How do I go about toggling these switches within my program? I have my onClick listener in my main activity, and the dialog creation as a different class. This may be wrong, but it works up to this point.</p>
  
  <p>MainActivity.java:</p>
  
  <pre><code>package com.example.arduinoautomation;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.View;
  import android.widget.Switch;
  
  public class MainActivity extends Activity {
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
  
  
      public void lampToggle(View view) {
          // Is the toggle on?
          boolean on = ((Switch) view).isChecked();
  
          if (on) {
              lampOnDialog lamp_on = new lampOnDialog();
              lamp_on.message = "Lamp is on.";
              lamp_on.show(this.getFragmentManager(),"switch");
          } else {
              // Disable vibrate
          }
      }
  
      public void lightToggle(View view) {
          // Is the toggle on?
          boolean on = ((Switch) view).isChecked();
  
          if (on) {
              lampOnDialog light_on = new lampOnDialog();
              light_on.message = "Light is on";
              light_on.show(this.getFragmentManager(), "switch");
          } else {
              // Disable vibrate
          }
      }
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
  }
  </code></pre>
  
  <p>lampOnDialog.java:</p>
  
  <pre><code>package com.example.arduinoautomation;
  
  import android.app.AlertDialog;
  import android.app.Dialog;
  import android.app.DialogFragment;
  import android.content.DialogInterface;
  import android.os.Bundle;
  
  public class lampOnDialog extends DialogFragment {
      String message = "";
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
  
          // Use the Builder class for convenient dialog construction
          AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
          builder.setMessage(message)
                 .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int id) {
                         // FIRE ZE MISSILES!
                     }
                 })
                 .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int id) {
                         // User cancelled the dialog
  
                     }
                 });
          // Create the AlertDialog object and return it
          return builder.create();
      }
  }
  </code></pre>
  
  <p>activity_main.xml</p>
  
  <pre><code>&lt;RelativeLayout 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=".MainActivity" &gt;
  
      &lt;Switch
          android:id="@+id/switch1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentTop="true"
          android:layout_marginTop="57dp"
          android:onClick="lampToggle"
          android:text="Lamp" /&gt;
  
      &lt;Switch
          android:id="@+id/switch2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_below="@+id/switch1"
          android:layout_marginTop="49dp"
          android:onClick="lightToggle"
          android:text="Lights" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>