Intro

This is part three of the Navigation drawer in Android post and it will be a demonstration on how to add drop-down items on a navigation drawer without using ExpandableListView.

The Exercise

We will continue working on the same project that we created for part one and part two of this post. First, in the drawermenu.xml file, we will add a new item called “members”.

From the previous project, we already know how to add an icon for the item. After this, we will create a new group of items:

If the logout item is not showing after you added this group, put the logout item in its own group like this:

This is how the drawermenu.xml file will look like after all that we added in it:

Now that our drawer menu is ready, we go to the Home activity class, to add the new items. First, we will declare a private boolean named isMembersVisible and initialize it with the value false.

Then in the setupUI method, we will set the visibility of the members’ group to false.

Next, we add the new items in the switchSreen(int id) method. Here we will use the private boolean variable that we created. We create an if/else statement,  so when we click on “Members” from the navigation drawer, first we check if the group is visible (and we know that it is not, because we set its visibility to false in the setupUI method), and we say that if it is not visible, when you click on it, set it to be visible, and also set the value to the boolean variable to true. So now we know that the group is visible and that the boolean value is true, so when we click on it again, it will not enter the if part, it will go in the else part, where we set it to false again, and we also set the boolean value to false also.

For the items in the group, we will just add toast messages to test if they are working.

This is how the Home Activity class will look like after all that:

You can find the whole post here: https://github.com/AleksandarGulevski/NavigationDrawerExercise/tree/masterThree

Happy coding!

 

 

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *