How to add title in Navigation drawer layout?

23,142

Solution 1

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"

Solution 2

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.

Share:
23,142

Related videos on Youtube

wawanopoulos
Author by

wawanopoulos

Updated on October 12, 2020

Comments

  • wawanopoulos
    wawanopoulos over 3 years

    [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

    • Enrique Diaz
      Enrique Diaz almost 11 years
      Do you have any idea about how to add several titles?
    • karlstackoverflow
      karlstackoverflow over 10 years
      Do you have a more complete code sample?
    • Georgi Angelov
      Georgi Angelov about 10 years
      Great solution!!! Thank you.
    • siddharth
      siddharth almost 9 years
      i also want to add this title. can you describe how can i do this?
  • CommonsWare
    CommonsWare almost 11 years
    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.
  • Blackbelt
    Blackbelt almost 11 years
    Yes I misunderstood him. Only now I realized he wants
  • Jacob Phillips
    Jacob Phillips almost 11 years
  • Lorenzo Barbagli
    Lorenzo Barbagli over 10 years
    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..