0

navigation drawer list class
  
  package com.example.navigation;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NavDrawerListAdapter extends BaseAdapter {
    
    private Context context;
    private ArrayList<NavDrawerItem> navDrawerItems;
     
    public NavDrawerListAdapter(Context context, ArrayList<NavDrawerItem> navDrawerItems){
        this.context = context;
        this.navDrawerItems = navDrawerItems;
    }

    @Override
    public int getCount() {
        return navDrawerItems.size();
    }

    @Override
    public Object getItem(int position) {       
        return navDrawerItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater)
                    context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.drawer_list_item, null);
        }
          
        ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
        TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
        
          
        imgIcon.setImageResource(navDrawerItems.get(position).getIcon());        
        txtTitle.setText(navDrawerItems.get(position).getTitle());
         
       
         
        return convertView;
    }

}
this is my main activity

package com.example.navigation;

import java.util.ArrayList;

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;


public class MainActivity extends ActionBarActivity {
 
  private DrawerLayout mDrawerLayout;
  private ListView mDrawerList;
  private ActionBarDrawerToggle mDrawerToggle;
  protected RelativeLayout _completeLayout, _activityLayout;
  // nav drawer title
  private CharSequence mDrawerTitle;

  // used to store app title
  private CharSequence mTitle;

  private ArrayList<NavDrawerItem> navDrawerItems;
  private NavDrawerListAdapter adapter;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   // if (savedInstanceState == null) {
   // // on first time display view for first nav item
   // // displayView(0);
   // }
  }

  public void set(String[] navMenuTitles, TypedArray navMenuIcons) {
   mTitle = mDrawerTitle = getTitle();

   mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
   mDrawerList = (ListView) findViewById(R.id.left_drawer);

   navDrawerItems = new ArrayList<NavDrawerItem>();

   // adding nav drawer items
   if (navMenuIcons == null) {
    for (int i = 0; i < navMenuTitles.length; i++) {
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[i]));
    }
   } else {
    for (int i = 0; i < navMenuTitles.length; i++) {
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[i],
       navMenuIcons.getResourceId(i, -1)));
    }
   }

   mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

   // setting the nav drawer list adapter
   adapter = new NavDrawerListAdapter(getApplicationContext(),
     navDrawerItems);
   mDrawerList.setAdapter(adapter);

   // enabling action bar app icon and behaving it as toggle button
   getSupportActionBar().setDisplayHomeAsUpEnabled(true);
   getSupportActionBar().setHomeButtonEnabled(true);
   // getSupportActionBar().setIcon(R.drawable.ic_drawer);

   mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
     R.drawable.ic_launcher, // nav menu toggle icon
     R.string.app_name, // nav drawer open - description for
     // accessibility
     R.string.app_name // nav drawer close - description for
   // accessibility
   ) {
    public void onDrawerClosed(View view) {
     getSupportActionBar().setTitle(mTitle);
     // calling onPrepareOptionsMenu() to show action bar icons
     supportInvalidateOptionsMenu();
    }

    public void onDrawerOpened(View drawerView) {
     getSupportActionBar().setTitle(mDrawerTitle);
     // calling onPrepareOptionsMenu() to hide action bar icons
     supportInvalidateOptionsMenu();
    }
   };
   mDrawerLayout.setDrawerListener(mDrawerToggle);

  }

  private class SlideMenuClickListener implements
    ListView.OnItemClickListener {
   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position,
     long id) {
    // display view for selected nav drawer item
    displayView(position);
   }
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
   // getSupportMenuInflater().inflate(R.menu.main, menu);
   return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {

   if (item.getItemId() == android.R.id.home) {
    if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
     mDrawerLayout.closeDrawer(mDrawerList);
    } else {
     mDrawerLayout.openDrawer(mDrawerList);
    }
   }

   return super.onOptionsItemSelected(item);
  }

  /***
   * Called when invalidateOptionsMenu() is triggered
   */
  @Override
  public boolean onPrepareOptionsMenu(Menu menu) {
   // if nav drawer is opened, hide the action items
   // boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
   // menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
   return super.onPrepareOptionsMenu(menu);
  }

  /**
   * Diplaying fragment view for selected nav drawer list item
   * */
  private void displayView(int position) {

   switch (position) {
   case 0:
    Intent intent = new Intent(this, FirstActivity.class);
    startActivity(intent);
    finish();// finishes the current activity
    break;
   case 1:
    Intent intent1 = new Intent(this, SecondActivity.class);
    startActivity(intent1);
    finish();// finishes the current activity
    break;
   // case 2:
   // Intent intent2 = new Intent(this, third.class);
   // startActivity(intent2);
   // finish();
   // break;
   // case 3:
   // Intent intent3 = new Intent(this, fourth.class);
   // startActivity(intent3);
   // finish();
   // break;
   // case 4:
   // Intent intent4 = new Intent(this, fifth.class);
   // startActivity(intent4);
   // finish();
   // break;
   // case 5:
   // Intent intent5 = new Intent(this, sixth.class);
   // startActivity(intent5);
   // finish();
   // break;
   default:
    break;
   }

   // update selected item and title, then close the drawer
   mDrawerList.setItemChecked(position, true);
   mDrawerList.setSelection(position);
   mDrawerLayout.closeDrawer(mDrawerList);
  }

  @Override
  public void setTitle(CharSequence title) {
   mTitle = title;
   getActionBar().setTitle(mTitle);
  }

  /**
   * When using the ActionBarDrawerToggle, you must call it during
   * onPostCreate() and onConfigurationChanged()...
   */

  @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);
   // Pass any configuration change to the drawer toggls
   mDrawerToggle.onConfigurationChanged(newConfig);
  }
 }
drawer item

package com.example.navigation;

public class NavDrawerItem {
 private String title;
 private int icon;

 public NavDrawerItem() {
 }

 public NavDrawerItem(String title, int icon) {
  this.title = title;
  this.icon = icon;
 }

 public NavDrawerItem(String title) {
  this.title = title;
 }

 public String getTitle() {
  return this.title;
 }

 public int getIcon() {
  return this.icon;
 }

 public void setTitle(String title) {
  this.title = title;
 }

 public void setIcon(int icon) {
  this.icon = icon;
 }

}

I created a navigation drawer menu item but now i want to create sub menu item in navigation drawer menu item. So how can i create this please suggest me thanks in advance

Here is my navigation Drawer menu item code which we r created in String.xml

<string-array name="nav_drawer_items">
        <item>First</item>
        <item>Second</item>
        <item>Third</item>
        <item>Fourth</item>
        <item>Fifth</item>
        <item>Sixth</item>
       
    </string-array>

2 Answers2

0

You need customize your navigation drawer menu. Actually looks like use a simple ListView, if you need subsecction, you should use ExpandableListView. Something like this:

If show your code or how you create the navigation drawer, we can help you with replace your actual menu with the custom menu.

encastellano
  • 435
  • 3
  • 5
  • 1
    i seen also this tutorial but i don't understand how to implement it on my code..so i show my navigation code here –  Mar 27 '15 at 10:01
  • Edit your post and show how to build your NavigationDrawer, since there are many ways to do it. In this case we can help you to customize your navigation drawer menu for show a expandablelistview instead a simple listview – encastellano Mar 27 '15 at 10:08
  • In this post show how to do it : http://stackoverflow.com/questions/18177447/expandablelistview-and-the-navigationdrawer – encastellano Mar 27 '15 at 10:19
0

Check the following link..

that helps you to create expandable listview in navigation drawer.

android-custom-navigation-drawer

You should check Andtroid Hive Example also.

Some more links that may be helpful to you.

Navigation drawer with Expandable list view - 1

Navigation drawer with Expandable list view - 2

Community
  • 1
  • 1
MDroid
  • 550
  • 4
  • 22
  • thanks sir for this tutorial its work nice.....now i want to add an activity before Navigation Drawer which contain all the menu of navigation drawer so when we click menu items then its open with same fragment class which are available in navigation item click and navigation drawer also for all activity –  Mar 27 '15 at 11:17