I am using the new Android Design Navigation Drawer. I want to add a switch in the drawer. Is there a away to implement this?
this is the menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="@+id/nav_view_menu_group_1"
android:checkableBehavior="single">
<item
android:id="@+id/nav_view_menu_item_myschedule"
android:icon="@drawable/ic_navview_my_schedule"
android:title="@string/navview_menu_item_myschedule"
android:titleCondensed="@string/navview_menu_item_myschedule" />
<item
android:id="@+id/nav_view_menu_item_iolive"
android:icon="@drawable/ic_navview_play_circle_fill"
android:title="@string/navview_menu_item_iolive"
android:titleCondensed="@string/navview_menu_item_iolive"
android:visible="false"/>
<item
android:id="@+id/nav_view_menu_item_explore"
android:icon="@drawable/ic_navview_explore"
android:title="@string/navview_menu_item_explore"
android:titleCondensed="@string/navview_menu_item_explore" />
<item
android:id="@+id/nav_view_menu_item_map"
android:icon="@drawable/ic_navview_map"
android:title="@string/navview_menu_item_map"
android:titleCondensed="@string/navview_menu_item_map"
android:visible="false"/>
</group>
</menu>
How can I change one
<item>
to be switch:
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Switch"
android:id="@+id/switch1"
android:layout_gravity="center_horizontal" />
I am currently using the default layout:
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/activity_main_drawer" />
Just Like this Image under Android Notification http://i.stack.imgur.com/M9nD7.png
I really Appreciate any feedback. Thank you.
We can do it by the following way
One way I have found of doing this would be to use
setActionView
on the menuItem you want:Probably want a click listener as well to change the state of the Switch:
I haven't tried, but you could probably use
android:actionLayout="@layout/switch_layout"
in xml and point to a custom layout you created.Also could try using an ActionProvider which might offer a little more robustness. I haven't tried this method either though.
If you only want to check for the changes in the switch, you can set onCheckedChangeListener only for the switch like so
If you want to be cool use lambda