Android Design Navigation Drawer - How to add a switch to nav xml?

I am using the new Android navigator for Android. I want to add a switch to the drawer. Is there any way to implement this?

this is the xml menu:

<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 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 shot under Android Notification http://i.stack.imgur.com/M9nD7.png enter image description here

I really appreciate any feedback. Thanks.

+6
source share
2 answers

One way I found is to use setActionView on the right menu:

  mNavigationView.getMenu().findItem(R.id.nav_connect) .setActionView(new Switch(this)); // To set whether switch is on/off use: ((Switch) mNavigationView.getMenu().findItem(R.id.nav_connect).getActionView()).setChecked(true); 

You probably want the click listener to also change the state of the switch:

  mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { switch (menuItem.getItemId()) { case R.id.nav_connect: ((Switch) menuItem.getActionView()).toggle(); return true; } } } 

I have not tried, but you could probably use android:actionLayout="@layout/switch_layout" in xml and point to the layout you created.

You can also try using ActionProvider , which can offer a little more reliability. I have not even tried this method.

+3
source

We can do this as follows:

 @Override public boolean onPrepareOptionsMenu(Menu menu) { //Get a reference to your item by id MenuItem item = menu.findItem(R.id.menu_pick_color); //Here, you get access to the view of your item, in this case, the layout of the item has a FrameLayout as root view but you can change it to whatever you use FrameLayout rootView = (FrameLayout)item.getActionView(); //Then you access to your control by finding it in the rootView YourControlClass control = (YourControlClass) rootView.findViewById(R.id.control_id); //And from here you can do whatever you want with your control return true; } 
+1
source

All Articles