Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Navigation Drawer ExpandableListView

I am trying to implement SubMenu under Navigation Drawer Menu. There will be only One group menu. See the following image what I want to do. Currently I have only added the main menu from a xml menu list. How to add an ExpandableListView to a Menu as SubMenu to get the following type of interface.

enter image description here

Here the code I used to open/close navigation drawer and start activity on drawer menu click

navigationView = (NavigationView) findViewById(R.id.navigation_view);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);

//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

            // This method will trigger on item Click of navigation menu
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {

                Intent intent;
                int id = item.getItemId();

                //Check to see which item was being clicked and perform appropriate action
                if (id == R.id.favorites) {
                    Intent favoritesIntent = new Intent(MainActivity.this, Favorites.class);
                    startActivity(favoritesIntent);
                } else if (id == R.id.settings) {
                    Intent settingsIntent = new Intent(MainActivity.this, Settings.class);
                    startActivity(settingsIntent);
                }

                drawerLayout.closeDrawer(GravityCompat.START);
                return true;

            }
        });

        // Initializing Drawer Layout and ActionBarToggle
        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openDrawer, R.string.closeDrawer){

            @Override
            public void onDrawerClosed(View drawerView) {
                // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank

                super.onDrawerOpened(drawerView);
            }
        };

        actionBarDrawerToggle.setDrawerIndicatorEnabled(false);
        ImageView titleLogo = (ImageView) findViewById(R.id.titleLogo);
        titleLogo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                drawerLayout.openDrawer(GravityCompat.START);

            }


      });
    .....

        @Override
            public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }

Here is the XML I used

<android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        app:itemTextColor="@color/white"
        app:itemIconTint="@color/white"
        app:headerLayout="@layout/header"
        app:menu="@menu/drawer"
        android:background="@color/colorPrimary" >

    </android.support.design.widget.NavigationView>

Here is the drawer.xml for menu list

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">

        <item
            android:id="@+id/favorites"
            android:checked="false"
            android:icon="@drawable/favorites"
            android:title="@string/favorites" />

        <item
            android:id="@+id/services"
            android:checked="false"
            android:icon="@drawable/services"
            android:title="@string/services" />

        <item
            android:id="@+id/settings"
            android:checked="false"
            android:icon="@drawable/settings"
            android:title="@string/settings" />

    </group>
</menu>
like image 416
Devil's Dream Avatar asked Mar 10 '17 17:03

Devil's Dream


1 Answers

If you want to achieve above behavior to your NavigationDrawer you can follow below steps:

1) You have to add Expandable listview to your NavigationView.

2) Create Header View

3) Create Child View.

4) Extend BaseExpandableListAdapter

5) Set Adapter in MainActivity

There are few solutions available. You can follow this tutorial or this. For basic knowledge about creating Navigation drawer, you can go here. I got helped by these tutorials and they are very easy to apply.

Hope this helps.

like image 184
tahsinRupam Avatar answered Nov 02 '22 21:11

tahsinRupam