title: Add listener to switch in action bar
body: <p>I have added switch to action bar of an activity like this</p>
  
  <p>switch layout.xml is</p>
  
  
  
  <pre><code> &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
      android:layout_height="match_parent"
     android:orientation="horizontal" &gt;
  
  &lt;Switch
      android:id="@+id/switch1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Menu.xml is</p>
  
  <pre><code>&lt;menu xmlns:android="http://schemas.android.com/apk/res/android" &gt;
  
  &lt;item
      android:id="@+id/action_settings"
      android:orderInCategory="100"
      android:showAsAction="never"
      android:title="@string/action_settings"/&gt;
  
   &lt;item
      android:id="@+id/myswitch"
      android:title=""
      android:showAsAction="always"
      android:actionLayout="@layout/switch_layout"
  /&gt;   
  &lt;/menu&gt;
  </code></pre>
  
  <p>in java file i have this code</p>
  
  <pre><code>    public boolean onCreateOptionsMenu(Menu menu)
    {
  
      getMenuInflater().inflate(R.menu.Menu, menu);
      switch1 = (Switch) findViewById(R.id.switch1);
      if(switch1 == null){
          Toast.makeText(this, "Null", Toast.LENGTH_SHORT).show();
      }
           //     switch1.setOnCheckedChangeListener(this);
         return true;
  }
  
  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         Toast.makeText(this, "Monitored switch is " + (isChecked ? "on" : "off"),
                 Toast.LENGTH_SHORT).show();
      }
  
  public boolean onOptionsItemSelected(MenuItem item)
  {
  
      switch (item.getItemId())
      {
      case R.id.switch1:
  
          Toast.makeText(this, "here", Toast.LENGTH_SHORT).show();
          break;
  
      default:
          break;
      }
      return super.onOptionsItemSelected(item);
  }
  </code></pre>
  
  <p>It is showing null in toast.
  How can i add listener to switch in menu??</p>