title: How to handle switch button in navigation drawer- Android Kotlin
body: <p>I  have a navigation Drawer menu in my app and in one item i added a switch button. However, i couldn't reach the switch button to handle it. Here is my Switch layout;</p>
  <p>(This switch button will be open or close notifications. In the image below, &quot;Bildirimler&quot; means notifications in Turkish.)</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;androidx.appcompat.widget.SwitchCompat
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:id=&quot;@+id/mySwitchID&quot;
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:layout_width=&quot;wrap_content&quot;
      android:layout_height=&quot;match_parent&quot;
      android:padding=&quot;64dp&quot;
      android:checked=&quot;true&quot;
      /&gt;
  </code></pre>
  <p>My Menu xml,</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;menu xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;
      &lt;item
          android:icon=&quot;@drawable/ic_launcher_foreground&quot;
          android:id=&quot;@+id/myFavourites&quot;
          android:title=&quot;Favorilerim&quot;
          /&gt;
      &lt;item
          android:icon=&quot;@drawable/ic_launcher_foreground&quot;
          android:id=&quot;@+id/rateUs&quot;
          android:title=&quot;Bizi Değerlendirin&quot;
          /&gt;
      &lt;item
          android:icon=&quot;@drawable/ic_launcher_foreground&quot;
          android:id=&quot;@+id/notifications&quot;
          android:title=&quot;Bildirimler&quot;
          app:actionLayout=&quot;@layout/menu_switch&quot;
          app:showAsAction=&quot;always&quot;
          /&gt;
      &lt;item
          android:icon=&quot;@drawable/ic_launcher_foreground&quot;
          android:id=&quot;@+id/item4&quot;
          android:title=&quot;item4&quot;
          /&gt;
      &lt;item
          android:icon=&quot;@drawable/ic_launcher_foreground&quot;
          android:id=&quot;@+id/item5&quot;
          android:title=&quot;item5&quot;
          /&gt;
  
      &lt;item android:title=&quot;Yana ok&quot; &gt;
          &lt;!-- Normalde bu yana ok yapar ama bizde alt başlık gibi olcak --&gt;
  
          &lt;menu&gt;
  
              &lt;item
                  android:id=&quot;@+id/item6&quot;
                  android:title=&quot;Yan1&quot;
                  android:icon=&quot;@drawable/ic_launcher_foreground&quot;
  
  
                  /&gt;
              &lt;item
                  android:id=&quot;@+id/item7&quot;
                  android:title=&quot;Yan2&quot;
                  android:icon=&quot;@drawable/ic_launcher_foreground&quot;
  
                  /&gt;
  
          &lt;/menu&gt;
  
      &lt;/item&gt;
  
  &lt;/menu&gt;
  
  </code></pre>
  <p>And my Main Activity,</p>
  <pre><code>class MainActivity : AppCompatActivity() {
      private lateinit var binding: ActivityMainBinding
      lateinit var toggle : ActionBarDrawerToggle
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          binding = ActivityMainBinding.inflate(layoutInflater)
          setContentView(binding.root)
  
          val drawerLayout : DrawerLayout = findViewById(R.id.drawerLayout)
          val navView : NavigationView = findViewById(R.id.nav_view)
  
          toggle = ActionBarDrawerToggle(this,drawerLayout,R.string.open,R.string.close)
          drawerLayout.addDrawerListener(toggle)
          toggle.syncState()
  
          supportActionBar?.setDisplayHomeAsUpEnabled(true)
  
          navView.setNavigationItemSelectedListener {
              when(it.itemId){
                  
                  R.id.notifications -&gt; {
  
  
                  }
  
              }
              true
          }
  
  
  
      }
  
      override fun onOptionsItemSelected(item: MenuItem): Boolean {
  
          if(toggle.onOptionsItemSelected(item)){
              return true
          }
          return super.onOptionsItemSelected(item)
  
  
      }
  
  }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/nVBQW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nVBQW.png" alt="enter image description here" /></a></p>
  <p>I saw some solutions for Java but not Kotlin.</p>