How to create a dropdown within a Navigation Drawer (in Android)?

AlleyOOP picture AlleyOOP · Mar 16, 2015 · Viewed 11.5k times · Source

I would like to be a able to create a Navigation Drawer that has some expandable, selectable items, and some non-expandable items. The consensus on StackOverflow for questions similar to mine point to solutions by ExpandableListView (which may not even apply to my idea). For the most part, what people are asking for is a way to separate items in the Nav Drawer like the GMail app does with labels, not what I'm trying to do...

...which is essentially outlined HERE enter image description here

and SIMILARLY HERE (though all, not some are dropdowns) . And not like THIS ANSWER.

Answer

AlleyOOP picture AlleyOOP · Jul 8, 2015

Use an ExpandableListView within a DrawerLayout, like so:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout2"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_commentary_behind_nav"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|top"
        android:text="Frame text" />
</FrameLayout>
<!-- The navigation drawer -->
    <ExpandableListView
        android:id="@+id/left_drawer2"
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/white"
        android:choiceMode="multipleChoice"
        android:dividerHeight="0dp"
        />

</android.support.v4.widget.DrawerLayout>

Then initialize in code like so:

    private DrawerLayout drawer;
    private ExpandableListView drawerList;
    private CheckBox checkBox;
    private ActionBarDrawerToggle actionBarDrawerToggle;


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drawer_layout);
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout2);
        drawerList = (ExpandableListView) findViewById(R.id.left_drawer2);
        drawerList.setAdapter(new NewAdapter(this, groupItem, childItem));
    }