Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Navigation Drawer SubMenu: How to Collapsible navigation items

I have a Navigation Drawer with 10 options Option #5 shoudl have another 7 options (like a sub menu) of some sort that is expandable/collapsible

How do I create a "Collapsible navigation items" like it is described here?

like image 677
Eric Bergman Avatar asked Jun 17 '13 14:06

Eric Bergman


People also ask

How do I customize my navigation drawer?

Android App Development for BeginnersStep 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 − Add the following code to res/layout/nav_header_main.

What is NavigationView in Android?

com.google.android.material.navigation.NavigationView. Represents a standard navigation menu for application. The menu contents can be populated by a menu resource file. NavigationView is typically placed inside a DrawerLayout .


1 Answers

Here is a sample application which makes it:

PrashamTrivedi / DrawerLayoutTest: Link is dead

EDIT: Simple Navigational Drawer Layout in Android

@Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
    {
        if (convertView == null)
        {
            LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.drawer_group_item,parent,false);
        }

        ((TextView) convertView).setText(groupItem.get(groupPosition));
        convertView.setTag(groupItem.get(groupPosition));
        return convertView;
    }

@Override
    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
    {
        tempChild = (ArrayList<String>) children.get(groupPosition);
        TextView text = null;

        if (convertView == null)
        {
            LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.drawer_submenu_item,parent,false);
        }

        text = (TextView) convertView;
        text.setText(tempChild.get(childPosition));

        convertView.setTag(tempChild.get(childPosition));
        return convertView;
}

And you have to create the new xml files in the layout folder (hint: create two, one for the group view and other for the submenu)

After all your side navigation must look like as below:

enter image description here

like image 148
cagcak Avatar answered Sep 16 '22 13:09

cagcak