Search code examples
androidnavigation-drawerandroid-support-libraryandroid-design-libraryandroiddesignsupport

Switch in Navigation drawer item with Design Support Library on Android


I need to put Switch inside item in navigation drawer. I'm using new design support library, but I cannot find if it is posibble at all. When using

android:checkable

item is just full selected and that is not what I wish.

This is screenshot of what I really want. Is that possible to achieve that?

enter image description here


Solution

  • Your menu item for the navigation drawer:

    <item
        android:id="@+id/nav_item1"
        android:icon="@drawable/ic_item1"
        android:title="item1"
        app:actionLayout="@layout/layout_switch"
        />
    

    and the layout for that item:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.v7.widget.SwitchCompat
            android:id="@+id/drawer_switch"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:text=""/>
    
    </LinearLayout>
    

    EDIT:

    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.