25

[UPDATE]

I solve the problem by adding addHeaderView :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTitle = mDrawerTitle = getTitle();
    mPlanetTitles = getResources().getStringArray(R.array.planets_array);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    LayoutInflater inflater = getLayoutInflater();
    ViewGroup mTop = (ViewGroup)inflater.inflate(R.layout.header_listview_menu, mDrawerList, false);
    mDrawerList.addHeaderView(mTop, null, false);

================================

My question is so simple !

I would like to how to add a title in a navigation drawer ?

I already created my navigation drawer with listview (icon+text) for each item.

Thanks a lot,

enter image description here

wawanopoulos
  • 9,614
  • 31
  • 111
  • 166

3 Answers3

11

You would do that the same way as you would add headings in any other ListView, by teaching your ListAdapter to return heading rows as well as detail rows. At the low level, this involves overriding methods like getViewTypeCount() and getItemViewType() in your ListAdapter, plus having getView() know the difference between the row types. Or, use an existing high-level implementation like https://github.com/emilsjolander/StickyListHeaders or http://code.google.com/p/android-amazing-listview/ or any of the others found when searching for android listview headers.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
5

Put a TextView above a ListView, and wrap it inside a vertical LinearLayout . Give to your ListView android:layout_weight="1" and android:layout_height="0dip"

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • That will work if the header is to be fixed at the top. The screenshot shows headers as part of the `ListView` itself (see "Title 2" row). Your solution may be what the OP is seeking, though. – CommonsWare Jun 05 '13 at 14:43
  • Yes I misunderstood him. Only now I realized he wants – Blackbelt Jun 05 '13 at 14:44
  • 1
    ooohhh..... never give 0dip tu height of a listview.. try it and see in DDMS how long does the method "onMeasure" takes to be executed.. – Lorenzo Barbagli Dec 07 '13 at 21:30
0

Maybe it's a bit late but I think I have a simpler solution. In your Activity's layout, instead of adding a listView inside the DrawerLayout, you can add for example a LinearLayout, and you can easily add separators and rows. For example:

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.astuetz.viewpager.extensions.PagerSlidingTabStrip
            android:id="@+id/indicator"
            android:layout_height="48dip"
            android:layout_width="fill_parent"/>

        <ViewPager
            android:id="@+id/pager"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

    </LinearLayout>

</RelativeLayout>

<LinearLayout 
    android:orientation="vertical"
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#111">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Separator 1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="First button"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Separator 2"/>

</LinearLayout>

And in the Activity, you can add the listeners to the buttons.

Hope that helps!

Racker
  • 1,316
  • 1
  • 11
  • 13