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.
Make sure to add id @+id/darkModeSwitch to your switch button. If XML creates automatically then just add the id to switch button. I hope it helps.
The user can view the navigation drawer when the user swipes a finger from the left edge of the activity. They can also find it from the home activity by tapping the app icon in the action bar. The drawer icon is displayed on all top-level destinations that use a DrawerLayout.
One way I have found of doing this would be to use setActionView
on the menuItem you want:
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);
Probably want a click listener as well to 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 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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With