user3311567
user3311567

Reputation: 61

Creating the Sub Menu in Navigation Drawer in Android

I have created the Navigation Drawer using the below link which is very nice :

http://blog.teamtreehouse.com/add-navigation-drawer-android

You can download the Navigation Drawer from this URl - https://github.com/sanjaisy/Android-Navigation-Drawer.git

Now I want to add Submenu to this navigation drawer.Please help me with a solution.

Here is my Full java Code

public class SmoothBanlanceHome extends ActionBarActivity {

        private ListView mDrawerList;
        private DrawerLayout mDrawerLayout;
        private ArrayAdapter<String> mAdapter;
        private ActionBarDrawerToggle mDrawerToggle;
        private String mActivityTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smooth_banlance_home);

        mDrawerList = (ListView)findViewById(R.id.navList);mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        mActivityTitle = getTitle().toString();

        addDrawerItems();
        setupDrawer();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
    }

        private void addDrawerItems() {
            String[] MenuArray = getResources().getStringArray(R.array.Naviagation_Menu_List);
            mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, MenuArray);
            mDrawerList.setAdapter(mAdapter);

            mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(SmoothBanlanceHome.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
                }
            });
        }

        private void setupDrawer() {
            mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {

                /** Called when a drawer has settled in a completely open state. */
                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);
                    getSupportActionBar().setTitle("Menu");
                    invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                }

                /** Called when a drawer has settled in a completely closed state. */
                public void onDrawerClosed(View view) {
                    super.onDrawerClosed(view);
                    getSupportActionBar().setTitle(mActivityTitle);
                    invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                }
            };

            mDrawerToggle.setDrawerIndicatorEnabled(true);
            mDrawerLayout.setDrawerListener(mDrawerToggle);
        }

        @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            // Sync the toggle state after onRestoreInstanceState has occurred.
            mDrawerToggle.syncState();
        }

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            mDrawerToggle.onConfigurationChanged(newConfig);
        }}

Here is the layout code

 <!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- android:layout_gravity="start" tells DrawerLayout to treat
         this as a sliding drawer on the left side for left-to-right
         languages and on the right side for right-to-left languages.
         The drawer is given a fixed width in dp and extends the full height of
         the container. A solid background is used for contrast
         with the content view. -->
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

Please can anyone tell me how can I make Sub menu with this code. You can also download the full Navigation Working code from git url as I have mention above.

Here is the picture which i want enter image description here

Upvotes: 2

Views: 25869

Answers (1)

Rodrigo Henriques
Rodrigo Henriques

Reputation: 1812

You should use NavigationView from Android Support Design Library instead of this NavigationDrawer.

Check this official sample:

https://github.com/chrisbanes/cheesesquare

It's quite easier.

Using Android Suppor Design Library you will create your submenus like this:

<?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/nav_home"
            android:icon="@drawable/ic_event"
            android:title="Home" />
        <item
            android:id="@+id/nav_profile"
            android:icon="@drawable/ic_dashboard"
            android:title="Perfil" />
    </group>

    <item android:title="More Options">
        <menu>
            <item
                android:icon="@drawable/ic_forum"
                android:title="Forum" />
            <item
                android:icon="@drawable/ic_headset"
                android:title="Headset" />
        </menu>
    </item>
</menu>

Best regards.

Upvotes: 7

Related Questions