18

I got a list view for navigation drawer, how can I put list item like "Add Friends", "Settings" align at the bottom of the list view.

enter image description here

Update 1
I add a setting view to the footer view.

View settingView = getLayoutInflater().inflate(R.layout.drawer_list_item, null);
TextView textView = (TextView)settingView.findViewById(R.id.drawer_list_item_text_view);
textView.setText("Settings");
drawerListView.addFooterView(settingView); 

The settings item appear at the last one in the list view successfully. But how can I make it to stick and align at the bottom? Thanks.

adneal
  • 30,484
  • 10
  • 122
  • 151
Edward Chiang
  • 1,133
  • 2
  • 12
  • 24

2 Answers2

21

You can put the ListView inside a RelativeLayout and assign android:layout_gravity="start" to the RelativeLayout. And set android:layout_alignParentBottom="true" property to the view you want to set to bottom of ListView.

This may help you.

<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" >

<FrameLayout
    android:id="@+id/content_frame"...../>

<RelativeLayout
    android:id="@+id/relative_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start" >

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#111"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <TextView
            android:id="@+id/text_view1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Add Friends" />

        <TextView
            android:id="@+id/text_view2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Settings" />
    </RelativeLayout>
</RelativeLayout>

</android.support.v4.widget.DrawerLayout>
Jay Donga
  • 431
  • 5
  • 16
  • Thank you, you gave me the key point solution, but I change a little bit for my need. I separated list view into upper list view and bottom list view, and add those two into left_drawer_layout to control drawer open and close. And the bottom list view set layout align parent bottom to true. – Edward Chiang Oct 09 '13 at 19:18
  • How to add this relative view in Activity? This is really helpful information. Thanks and repped! – imcc Feb 07 '14 at 00:42
  • 5
    I am getting classCastException here – Anand Phadke Feb 11 '14 at 13:24
  • 5
    @andrewww the exception comes because you are using the `ListView` reference when calling `mDrawerLayout.isDrawerOpen()` and open/close. You should use the reference to the `relative_layout`. – Saran Feb 15 '14 at 15:10
2

Jay Donga was really helpful, but I had to change two methods to insert a footer in my DrawerLayout.

// WITHOUT FOOTER VIEW
mDrawerLayout.isDrawerOpen(mDrawerList)
mDrawerLayout.closeDrawer(mDrawerList)

// WITH FOOTER VIEW
mDrawerContent = (RelativeLayout) findViewById(R.id.relative_layout);
mDrawerLayout.isDrawerOpen(mDrawerContent)
mDrawerLayout.closeDrawer(mDrawerContent)
Clinton Dsouza
  • 330
  • 4
  • 20
AMarones
  • 1,203
  • 11
  • 9