6

Im trying to add a switch (checkbox as second option) to the navigation drawer. The "slide in menu". The default one you'll get when creating a new project with navigation drawer.

I've tried on a fresh new project so I dont mess up my 'real' project.

I tried this from SO

But without any luck. Cant seem to find anything else worth mentioning..

Im trying to add the switch at the last menuItem. activity_main_drawer.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_camera"
        android:icon="@drawable/ic_menu_camera"
        android:title="Import"
        android:checkable="true"/>
    <item
        android:id="@+id/nav_gallery"
        android:icon="@drawable/ic_menu_gallery"
        android:title="Gallery" />
    <item
        android:id="@+id/nav_slideshow"
        android:icon="@drawable/ic_menu_slideshow"
        android:title="Slideshow" />
    <item
        android:id="@+id/nav_manage"
        android:icon="@drawable/ic_menu_manage"
        android:title="Tools" />
</group>

<item android:title="Communicate">
    <menu>
        <item
            android:id="@+id/nav_share"
            android:icon="@drawable/ic_menu_share"
            android:title="Share" />
        <item
            android:id="@+id/nav_send"
            android:icon="@drawable/ic_menu_send"
            android:title="Send" />
        <item
            android:id="@+id/myswitch"
            android:title=""
            android:actionLayout="@layout/ttt"
            />
    </menu>
</item>
</menu>

ttt.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

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

</RelativeLayout>

The last item "id/myswitch" doens't show at all. The MainActivity.java is 100% default. Thats why I dont post it.

Community
  • 1
  • 1
Frid
  • 61
  • 1
  • 5

1 Answers1

5

Instead of:

<item
   android:id="@+id/myswitch"
   android:title=""
   android:actionLayout="@layout/ttt"
   />

write:

<item
   android:id="@+id/myswitch"
   android:title=""
   app:actionLayout="@layout/ttt"
   />

Change android:actionLayout to app:actionLayout.

lenooh
  • 10,364
  • 5
  • 58
  • 49
  • 3
    How do you add a listener to the switch in the navigation drawer. – codemoonger Jun 26 '17 at 18:34
  • How do you add a listener to the switch in the navigation drawer? – rochasdv Feb 22 '18 at 13:32
  • rochasdv & mogren3000: I ended up using a different approach. In fact, I found out that you can use *any* view in the drawer, so there's no point in bothering with the menu stuff. Just create a view the usual way (with listeners, etc.) and add in to the drawer. – lenooh Feb 22 '18 at 19:52