2016-07-09

This post will walk you through how to create a custom menu item in the navigation drawer menu, with a toggle on and off button on the right using app:actionLayout and a custom layout with SwitchCompat.

1. Create an sample android app from the Android Studio and choose navigation drawer as your default template.

2. Create a layout file action_view_switch.xml

3. Replace the content in the res/menu/activity_main_drawer.xml with the following

4. Add a class field in MainActivity.java

5. Add the following code in the onCreate method in MainActivity.java

6. Replace the code in onNavigationItemSelected in MainActivity.java with the following.

7. Run the app, open the navigation drawer menu, you should see the second item on the menu has a toggle on off button on the right.

References:

https://github.com/codepath/android_guides/wiki/Fragment-Navigation-Drawer

http://stackoverflow.com/questions/37361091/android-something-like-navigation-drawer-with-custom-filling

Show more