title: Adding headers to navigation drawer
body: <p>I've been trying to add headers/sections to my navigation drawer, but unfortunately unsuccessfully.
  I looked in those questions here in StackOverFlow :</p>
  
  <p><a href="https://stackoverflow.com/questions/19341033/android-navigation-drawer-listview-headers">Android Navigation Drawer ListView Headers</a></p>
  
  <p><a href="https://stackoverflow.com/questions/17170396/navigation-drawer-with-headers-sections">Navigation Drawer with Headers/Sections</a></p>
  
  <p>Here's what I am trying to do.
  I want to make a navigation drawer having (Icons and text in each row), and have between some headers.(for example in item 2 and item 6).
  I'd appreciate if you tell me what to add/change in my code, to make it possible.(but more detailed then "Override function X and Override function Y", cause I am a beginner and I'm having bad time trying to implement it to my code → I've tried 6 times today, but It crashes).</p>
  
  <p>MainActivity:</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  
  import android.app.Activity;
  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.view.LayoutInflater;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.ListView;
  import android.widget.SectionIndexer;
  
  public class MainActivity extends Activity {
      public String[] drawerListViewItems;
      public ListView drawerListView;
      public ActionBarDrawerToggle actionBarDrawerToggle;
      public DrawerLayout drawerLayout;
      TypedArray menuIcons;
  // nav drawer title
  private CharSequence mDrawerTitle;
  private CharSequence mTitle;
  private List&lt;RowItem&gt; rowItems;
  private CustomAdapter adapter;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      // organize the title of the navigation drawer
      mTitle = mDrawerTitle = getTitle();
  
      // get list items from strings.xml
      drawerListViewItems = getResources().getStringArray(
              R.array.Navigation_Drawer);
      // get list icons from the strings
      menuIcons = getResources().obtainTypedArray(R.array.icons);
  
      // get ListView defined in activity_main.xml
      drawerListView = (ListView) findViewById(R.id.left_drawer);
  
      // Set the adapter for the list view
      drawerListView.setAdapter(new ArrayAdapter&lt;String&gt;(this,
              R.layout.drawer_listview_item, drawerListViewItems));
      // row items array list "RowItem" it's a class
      rowItems = new ArrayList&lt;RowItem&gt;();
      // adds the icons to each item sync
      for (int i = 0; i &lt; drawerListViewItems.length; i++) {
          RowItem items = new RowItem(drawerListViewItems[i],
                  menuIcons.getResourceId(i, -1));
          rowItems.add(items);
      }
  
      drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
  
      /*
       * LayoutInflater inflater = getLayoutInflater(); ViewGroup mTop =
       * (ViewGroup)inflater.inflate(R.layout.flats, drawerListView, false);
       * drawerListView.addHeaderView(mTop);
       */
  
      actionBarDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
      drawerLayout, /* DrawerLayout object from line 31 */
      R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
      R.string.drawer_open, /* "open drawer" description */
      R.string.drawer_close /* "close drawer" description */
      ) {
          public void onDrawerClosed(View view) {
              getActionBar().setTitle(mTitle);
              // calling onPrepareOptionsMenu() to show action bar icons
              invalidateOptionsMenu();
          }
          public void onDrawerOpened(View drawerView) {
              getActionBar().setTitle(mDrawerTitle);
              // calling onPrepareOptionsMenu() to hide action bar icons
              invalidateOptionsMenu();
          }
      };
  
      menuIcons.recycle();
  
      adapter = new CustomAdapter(getApplicationContext(), rowItems);
      drawerListView.setAdapter(adapter);  
      drawerLayout.setDrawerListener(actionBarDrawerToggle); 
      getActionBar().setDisplayHomeAsUpEnabled(true);
  
      drawerListView.setOnItemClickListener(new DrawerItemClickListener());
  
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
  
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }   
  @Override
  public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      actionBarDrawerToggle.onConfigurationChanged(newConfig);
  }   
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
  
      // call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns
      // true
      // then it has handled the app icon touch event
      if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
          return true;
      }
      return super.onOptionsItemSelected(item);
  }   
  @Override
  protected void onPostCreate(Bundle savedInstanceState) {
      super.onPostCreate(savedInstanceState);
      // Sync the toggle state after onRestoreInstanceState has occurred.
      actionBarDrawerToggle.syncState();
  }  
  private class DrawerItemClickListener implements
          ListView.OnItemClickListener {
      @Override
      public void onItemClick(AdapterView parent, View view, int position,
              long id) {
          SelectItem(position);
      }   
  }
  
  public void SelectItem(int position) {
      // TODO Auto-generated method stub
      switch (position) {
      case 0:
          setTitle(drawerListViewItems[position]);
          drawerLayout.closeDrawer(drawerListView);
          break;
      case 1:
          setTitle(drawerListViewItems[position]);
          drawerLayout.closeDrawer(drawerListView);
          break;
      case 2:
          setTitle(drawerListViewItems[position]);
          drawerLayout.closeDrawer(drawerListView);
          break;
      case 3:
          setTitle(drawerListViewItems[position]);
          drawerLayout.closeDrawer(drawerListView);
          break;
      case 4:
          setTitle(drawerListViewItems[position]);
          drawerLayout.closeDrawer(drawerListView);
          break;
      case 5:
          setTitle(drawerListViewItems[position]);
          drawerLayout.closeDrawer(drawerListView);
          break;
      case 6:
          setTitle(drawerListViewItems[position]);
          drawerLayout.closeDrawer(drawerListView);
          break;
      case 7:
          setTitle(drawerListViewItems[position]);
          drawerLayout.closeDrawer(drawerListView);
          break;
      case 8:
          setTitle(drawerListViewItems[position]);
          drawerLayout.closeDrawer(drawerListView);
          break;
      default:
          break;
      }   
  }
  
  @Override
  public void setTitle(CharSequence title) {
      mTitle = title;
      getActionBar().setTitle(mTitle);
  }   
  }
  </code></pre>
  
  <p>CustomAdapter:</p>
  
  <pre><code>import java.util.List;
  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 CustomAdapter extends BaseAdapter {
  Context context;
  List&lt;RowItem&gt; rowItem;
  
  CustomAdapter(Context context, List&lt;RowItem&gt; rowItem) {
      this.context = context;
      this.rowItem = rowItem;
  }
  
  private class ViewHolder {
      ImageView icon;
      TextView title;
  }
  
  @Override
  public int getViewTypeCount() {
      return super.getViewTypeCount();
  }
  
  @Override
  public int getItemViewType(int position) {
      return super.getItemViewType(position);
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
  
  ViewHolder holder = null;
  
  LayoutInflater mInflater = (LayoutInflater) context
          .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
  if (convertView == null) {
      convertView = mInflater.inflate(R.layout.list_item, null);
      holder = new ViewHolder();
      holder.icon = (ImageView) convertView.findViewById(R.id.icon);
      holder.title = (TextView) convertView.findViewById(R.id.title);
  
      RowItem row_pos = rowItem.get(position);
      // setting the image resource and title
      holder.icon.setImageResource(row_pos.getIcon());
      holder.title.setText(row_pos.getTitle());
      convertView.setTag(holder);
  } else {
      holder = (ViewHolder) convertView.getTag();
  }
  return convertView;
  }
  @Override
  public int getCount() {
      return rowItem.size();
  }
  @Override
  public Object getItem(int position) {
      return rowItem.get(position);
  }
  @Override
  public long getItemId(int position) {
      return rowItem.indexOf(getItem(position));
  
   }
  
  }
  </code></pre>
  
  <p>RowItem:</p>
  
  <pre><code>  public class RowItem {
  
  private String title;
  private int icon;
  String ItemName;
  public RowItem(String title, int icon) {
      this.title = title;
      this.icon = icon;
  }
  public String getItemName() {
      return ItemName;
  }
  public void setItemName(String itemName) {
      ItemName = itemName;
  }
  public String getTitle() {
      return title;
  }
  public void setTitle(String title) {
      this.title = title;
  }
  public int getIcon() {
      return icon;
  }
  public void setIcon(int icon) {
      this.icon = icon;
  }
  }
  </code></pre>
  
  <p>drawerlistviewitem.xml</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="48dp"
      android:padding="2dp" &gt;
      &lt;ImageView
          android:id="@+id/icon"
          android:layout_width="50dp"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_centerVertical="true"
          android:layout_marginLeft="12dp"
          android:layout_marginRight="12dp"
          android:contentDescription="desc"
          android:src="@drawable/firstlogo" /&gt;
      &lt;TextView
          android:id="@+id/title"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_marginTop="10dp"
          android:layout_toRightOf="@id/icon"
          android:gravity="center_vertical"
          android:text="abccc"
          android:textColor="#000000"
          android:textSize="20sp" /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>