In this tutorial, we’ll be implementing an application in which we display an ExpandableListView of items inside a NavigationView.

Android ExpandableListView inside NavigationView

As we’d seen in the NavigationView tutorial, we can have submenus but those ones can’t be expanded/collapsed like we do in ExpandableListViews. So let’s try to implement the ExpandableListView inside a NavigationView.

Let’s start by creating a new Android Studio Project. Select the activity template as Navigation Drawer Activity as shown below.

android-navigation-expandable-view-select-template

This encloses our activity class in a DrawerLayout and adds a NavigationView inside it by default. In our xml editor we see the following design of the activity:

android-navigation-expandable-view-select-template

Now, all we need to do is replace the menus with an ExpandableListView.

Android ExpandableListView inside NavigationView Project Structure

android-webview-bookmarks-project-structure

In this application each of our menus while open a url in a WebView. Some menus can be expanded while others cannot. We’ll populate the data using the class MenuModel.

Android ExpandableListView NavigationView Code

Let’s look at the layout file activity_main.xml

The code for the content_main.xml class is given below.

The code for the layouts list_group_header.xml and list_group_child.xml which will be inflated in the Adapter class is given below.

list_group_header.xml

list_group_child.xml

The code for the MenuModel.java is given below.

The same class can be used for both the header and child rows of the ExpandableListView in this tutorial.

The code for the MainActivity.java class is given below.

prepareMenuData() is where we are populating our data structures with dummy datas.

For the group headers, we use an ArrayList of MenuModels.

The child rows data is populated in a HashMap where the key is the header MenuModel and the values are the list of child MenuModels.

In the onGroupClick and onChildClick Listeners of the ExpandableListView, we retrieve the url for the current position and load them in the WebView of our Activity.

Let’s look at the ExpandableListAdapter.java class.

Don’t forget to add the following permission in your AndroidManifest.xml file.

Android ExpandableListView NavigationView App Output

The output of the above application in action is given below.
android navigationview expandable view app output

This brings an end to this tutorial. You can download the final Android NavigationViewExpandableListView Tutorial from the link below.

By admin

Leave a Reply

%d bloggers like this: